Java 构造方法详解
一、构造方法概述
1.1 什么是构造方法
<mxfile>
<diagram id="constructor-role" name="构造方法的作用">
<mxGraphModel dx="900" dy="680" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="680">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="2" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#64748B;strokeWidth=2;" vertex="1" parent="1">
<mxGeometry x="30" y="20" width="840" height="640" as="geometry" />
</mxCell>
<mxCell id="3" value="🔧 构造方法的作用" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=24;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
<mxGeometry x="30" y="20" width="840" height="55" as="geometry" />
</mxCell>
<mxCell id="4" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EDE9FE;strokeColor=#8B5CF6;strokeWidth=2;" vertex="1" parent="1">
<mxGeometry x="60" y="95" width="780" height="60" as="geometry" />
</mxCell>
<mxCell id="5" value="构造方法(Constructor)是一种特殊的方法,在创建对象时自动执行" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=16;fontColor=#5B21B6;fontStyle=1;" vertex="1" parent="1">
<mxGeometry x="60" y="95" width="780" height="60" as="geometry" />
</mxCell>
<mxCell id="6" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#22C55E;strokeWidth=3;shadow=1;" vertex="1" parent="1">
<mxGeometry x="60" y="175" width="240" height="200" as="geometry" />
</mxCell>
<mxCell id="7" value="1️⃣ 创建对象时自动调用" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
<mxGeometry x="60" y="175" width="240" height="40" as="geometry" />
</mxCell>
<mxCell id="8" value="" style="shape=image;verticalLabelPosition=bottom;labelBackgroundColor=default;verticalAlign=top;aspect=fixed;imageAspect=0;image=data:image/svg+xml,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyNCAyNCIgZmlsbD0iIzIyQzU1RSI+PHBhdGggZD0iTTEyIDJDNi40OCAyIDIgNi40OCAyIDEyczQuNDggMTAgMTAgMTAgMTAtNC40OCAxMC0xMFMxNy41MiAyIDEyIDJ6bTAgMThjLTQuNDEgMC04LTMuNTktOC04czMuNTktOCA4LTggOCAzLjU5IDggOC0zLjU5IDgtOCA4em0tMi0zLjVsNi0zLjUtNi0zLjV2N3oiLz48L3N2Zz4=;" vertex="1" parent="1">
<mxGeometry x="155" y="225" width="50" height="50" as="geometry" />
</mxCell>
<mxCell id="9" value="new 关键字触发" style="text;html=1;strokeColor=none;fillColor=#BBF7D0;align=center;verticalAlign=middle;fontSize=13;fontColor=#166534;rounded=1;fontStyle=1;" vertex="1" parent="1">
<mxGeometry x="100" y="285" width="160" height="28" as="geometry" />
</mxCell>
<mxCell id="10" value="无需手动调用,自动执行" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontColor=#166534;" vertex="1" parent="1">
<mxGeometry x="80" y="320" width="200" height="25" as="geometry" />
</mxCell>
<mxCell id="11" value="Person p = new Person();" style="text;html=1;strokeColor=none;fillColor=#1E293B;align=center;verticalAlign=middle;fontSize=11;fontColor=#22C55E;fontFamily=Consolas;rounded=1;" vertex="1" parent="1">
<mxGeometry x="75" y="348" width="210" height="22" as="geometry" />
</mxCell>
<mxCell id="12" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#3B82F6;strokeWidth=3;shadow=1;" vertex="1" parent="1">
<mxGeometry x="330" y="175" width="240" height="200" as="geometry" />
</mxCell>
<mxCell id="13" value="2️⃣ 初始化对象的属性" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
<mxGeometry x="330" y="175" width="240" height="40" as="geometry" />
</mxCell>
<mxCell id="14" value="name" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#60A5FA;strokeColor=#3B82F6;strokeWidth=1;fontSize=11;fontColor=#FFFFFF;" vertex="1" parent="1">
<mxGeometry x="360" y="230" width="55" height="25" as="geometry" />
</mxCell>
<mxCell id="15" value="age" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#60A5FA;strokeColor=#3B82F6;strokeWidth=1;fontSize=11;fontColor=#FFFFFF;" vertex="1" parent="1">
<mxGeometry x="422" y="230" width="55" height="25" as="geometry" />
</mxCell>
<mxCell id="16" value="id" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#60A5FA;strokeColor=#3B82F6;strokeWidth=1;fontSize=11;fontColor=#FFFFFF;" vertex="1" parent="1">
<mxGeometry x="484" y="230" width="55" height="25" as="geometry" />
</mxCell>
<mxCell id="17" value="↓" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=18;fontColor=#3B82F6;fontStyle=1;" vertex="1" parent="1">
<mxGeometry x="430" y="255" width="40" height="25" as="geometry" />
</mxCell>
<mxCell id="18" value="赋予初始值" style="text;html=1;strokeColor=none;fillColor=#BFDBFE;align=center;verticalAlign=middle;fontSize=13;fontColor=#1E40AF;rounded=1;fontStyle=1;" vertex="1" parent="1">
<mxGeometry x="370" y="285" width="160" height="28" as="geometry" />
</mxCell>
<mxCell id="19" value="确保对象状态有效可用" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontColor=#1E40AF;" vertex="1" parent="1">
<mxGeometry x="350" y="320" width="200" height="25" as="geometry" />
</mxCell>
<mxCell id="20" value="this.name = name;" style="text;html=1;strokeColor=none;fillColor=#1E293B;align=center;verticalAlign=middle;fontSize=11;fontColor=#60A5FA;fontFamily=Consolas;rounded=1;" vertex="1" parent="1">
<mxGeometry x="355" y="348" width="190" height="22" as="geometry" />
</mxCell>
<mxCell id="21" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=3;shadow=1;" vertex="1" parent="1">
<mxGeometry x="600" y="175" width="240" height="200" as="geometry" />
</mxCell>
<mxCell id="22" value="3️⃣ 执行必要的启动操作" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
<mxGeometry x="600" y="175" width="240" height="40" as="geometry" />
</mxCell>
<mxCell id="23" value="⚙️" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=36;" vertex="1" parent="1">
<mxGeometry x="695" y="220" width="50" height="50" as="geometry" />
</mxCell>
<mxCell id="24" value="资源准备 / 连接建立" style="text;html=1;strokeColor=none;fillColor=#FDE68A;align=center;verticalAlign=middle;fontSize=13;fontColor=#92400E;rounded=1;fontStyle=1;" vertex="1" parent="1">
<mxGeometry x="640" y="285" width="160" height="28" as="geometry" />
</mxCell>
<mxCell id="25" value="打开文件、连接数据库等" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontColor=#92400E;" vertex="1" parent="1">
<mxGeometry x="620" y="320" width="200" height="25" as="geometry" />
</mxCell>
<mxCell id="26" value="loadConfig();" style="text;html=1;strokeColor=none;fillColor=#1E293B;align=center;verticalAlign=middle;fontSize=11;fontColor=#FBBF24;fontFamily=Consolas;rounded=1;" vertex="1" parent="1">
<mxGeometry x="655" y="348" width="130" height="22" as="geometry" />
</mxCell>
<mxCell id="27" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FCE7F3;strokeColor=#EC4899;strokeWidth=3;shadow=1;" vertex="1" parent="1">
<mxGeometry x="60" y="400" width="780" height="240" as="geometry" />
</mxCell>
<mxCell id="28" value="💡 形象类比:构造方法就像【出生证明】" style="text;html=1;strokeColor=none;fillColor=#EC4899;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
<mxGeometry x="60" y="400" width="780" height="40" as="geometry" />
</mxCell>
<mxCell id="29" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#F9A8D4;strokeWidth=2;" vertex="1" parent="1">
<mxGeometry x="90" y="460" width="320" height="160" as="geometry" />
</mxCell>
<mxCell id="30" value="👶 婴儿出生" style="text;html=1;strokeColor=none;fillColor=#F472B6;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
<mxGeometry x="90" y="460" width="120" height="30" as="geometry" />
</mxCell>
<mxCell id="31" value="📋 出生证明" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FDF2F8;strokeColor=#EC4899;strokeWidth=2;fontSize=13;fontStyle=1;fontColor=#BE185D;" vertex="1" parent="1">
<mxGeometry x="120" y="505" width="120" height="40" as="geometry" />
</mxCell>
<mxCell id="32" value="姓名:小明
出生日期:2024-01-01
性别:男" style="text;html=1;strokeColor=none;fillColor=#FDF2F8;align=left;verticalAlign=middle;fontSize=11;fontColor=#9D174D;rounded=1;" vertex="1" parent="1">
<mxGeometry x="120" y="555" width="130" height="55" as="geometry" />
</mxCell>
<mxCell id="33" value="一出生就要
填写基本信息" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontColor=#BE185D;fontStyle=2;" vertex="1" parent="1">
<mxGeometry x="260" y="520" width="130" height="40" as="geometry" />
</mxCell>
<mxCell id="34" value="=" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=36;fontColor=#EC4899;fontStyle=1;" vertex="1" parent="1">
<mxGeometry x="420" y="510" width="60" height="50" as="geometry" />
</mxCell>
<mxCell id="35" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#F9A8D4;strokeWidth=2;" vertex="1" parent="1">
<mxGeometry x="490" y="460" width="330" height="160" as="geometry" />
</mxCell>
<mxCell id="36" value="🖥️ 对象创建" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
<mxGeometry x="490" y="460" width="120" height="30" as="geometry" />
</mxCell>
<mxCell id="37" value="🔧 构造方法" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EDE9FE;strokeColor=#8B5CF6;strokeWidth=2;fontSize=13;fontStyle=1;fontColor=#5B21B6;" vertex="1" parent="1">
<mxGeometry x="520" y="505" width="120" height="40" as="geometry" />
</mxCell>
<mxCell id="38" value="name = "小明"
age = 0
id = 1001" style="text;html=1;strokeColor=none;fillColor=#EDE9FE;align=left;verticalAlign=middle;fontSize=11;fontColor=#5B21B6;rounded=1;fontFamily=Consolas;" vertex="1" parent="1">
<mxGeometry x="520" y="555" width="130" height="55" as="geometry" />
</mxCell>
<mxCell id="39" value="对象诞生时
自动初始化属性" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontColor=#5B21B6;fontStyle=2;" vertex="1" parent="1">
<mxGeometry x="665" y="520" width="130" height="40" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
1.2 基本语法
[访问修饰符] 类名(参数列表) {
// 初始化代码
}
public class Student {
private String name;
private int age;
// 构造方法
public Student(String name, int age) {
this.name = name;
this.age = age;
}
}
// 使用
Student stu = new Student("张三", 20);
1.3 构造方法 vs 普通方法
| 对比项 | 构造方法 | 普通方法 |
|---|---|---|
| 命名 | 必须与类名相同 | 任意合法标识符 |
| 返回值 | 无(连 void 都没有) | 必须声明返回类型 |
| 调用时机 | new 时自动调用 | 手动调用 |
| 调用次数 | 每个对象只调用一次 | 可多次调用 |
| 作用 | 初始化对象 | 执行功能 |
| 继承 | 不能被继承 | 可以被继承 |
| 重写 | 不能重写 | 可以重写 |
二、默认构造方法
2.1 编译器自动生成
<mxfile>
<diagram id="default-constructor-rule" name="默认构造方法规则">
<mxGraphModel dx="1281" dy="581" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="800" pageHeight="500" math="0" shadow="0">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="2" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#64748B;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="460" width="760" x="20" y="20" as="geometry" />
</mxCell>
<mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=22;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="📐 默认构造方法规则" vertex="1">
<mxGeometry height="50" width="760" x="20" y="20" as="geometry" />
</mxCell>
<mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEE2E2;strokeColor=#EF4444;strokeWidth=3;shadow=1;" value="" vertex="1">
<mxGeometry height="160" width="300" x="50" y="90" as="geometry" />
</mxCell>
<mxCell id="5" parent="1" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="📝 类中没有定义构造方法" vertex="1">
<mxGeometry height="35" width="300" x="50" y="90" as="geometry" />
</mxCell>
<mxCell id="6" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#475569;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="95" width="260" x="70" y="140" as="geometry" />
</mxCell>
<mxCell id="7" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#E2E8F0;fontFamily=Consolas;" value="public class Person {" vertex="1">
<mxGeometry height="20" width="230" x="85" y="150" as="geometry" />
</mxCell>
<mxCell id="8" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#94A3B8;fontFamily=Consolas;" value=" String name;" vertex="1">
<mxGeometry height="20" width="230" x="85" y="172" as="geometry" />
</mxCell>
<mxCell id="9" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#94A3B8;fontFamily=Consolas;" value=" int age;" vertex="1">
<mxGeometry height="20" width="230" x="85" y="194" as="geometry" />
</mxCell>
<mxCell id="10" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#E2E8F0;fontFamily=Consolas;" value="}" vertex="1">
<mxGeometry height="20" width="230" x="85" y="216" as="geometry" />
</mxCell>
<mxCell id="11" parent="1" style="text;html=1;strokeColor=none;fillColor=#FECACA;align=center;verticalAlign=middle;fontSize=11;fontColor=#991B1B;rounded=1;fontStyle=1;" value="❌ 无构造方法" vertex="1">
<mxGeometry height="20" width="100" x="210" y="172" as="geometry" />
</mxCell>
<mxCell id="12" edge="1" parent="1" style="shape=flexArrow;endArrow=classic;html=1;strokeWidth=2;fillColor=#F59E0B;strokeColor=#D97706;width=20;endSize=8;" value="">
<mxGeometry relative="1" as="geometry">
<mxPoint x="360" y="170" as="sourcePoint" />
<mxPoint x="450" y="170" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEF3C7;align=center;verticalAlign=middle;fontSize=12;fontColor=#92400E;rounded=1;fontStyle=1;" value="编译时" vertex="1">
<mxGeometry height="22" width="60" x="375" y="140" as="geometry" />
</mxCell>
<mxCell id="14" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#22C55E;strokeWidth=3;shadow=1;" value="" vertex="1">
<mxGeometry height="160" width="310" x="450" y="90" as="geometry" />
</mxCell>
<mxCell id="15" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="✨ 编译器自动添加无参构造" vertex="1">
<mxGeometry height="35" width="310" x="450" y="90" as="geometry" />
</mxCell>
<mxCell id="16" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#475569;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="95" width="270" x="470" y="140" as="geometry" />
</mxCell>
<mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#E2E8F0;fontFamily=Consolas;" value="public class Person {" vertex="1">
<mxGeometry height="20" width="240" x="485" y="150" as="geometry" />
</mxCell>
<mxCell id="18" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#4ADE80;fontFamily=Consolas;fontStyle=1;" value=" public Person() { }" vertex="1">
<mxGeometry height="20" width="240" x="485" y="172" as="geometry" />
</mxCell>
<mxCell id="19" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#94A3B8;fontFamily=Consolas;" value=" String name;" vertex="1">
<mxGeometry height="20" width="240" x="485" y="194" as="geometry" />
</mxCell>
<mxCell id="20" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#E2E8F0;fontFamily=Consolas;" value="}" vertex="1">
<mxGeometry height="20" width="240" x="485" y="216" as="geometry" />
</mxCell>
<mxCell id="21" parent="1" style="text;html=1;strokeColor=none;fillColor=#BBF7D0;align=center;verticalAlign=middle;fontSize=11;fontColor=#166534;rounded=1;fontStyle=1;" value="✅ 自动生成" vertex="1">
<mxGeometry height="20" width="90" x="640" y="174" as="geometry" />
</mxCell>
<mxCell id="22" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#3B82F6;strokeWidth=3;shadow=1;" value="" vertex="1">
<mxGeometry height="190" width="710" x="50" y="270" as="geometry" />
</mxCell>
<mxCell id="23" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="💡 核心要点" vertex="1">
<mxGeometry height="35" width="130" x="50" y="270" as="geometry" />
</mxCell>
<mxCell id="24" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F1F5F9;strokeColor=#94A3B8;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="120" width="200" x="70" y="320" as="geometry" />
</mxCell>
<mxCell id="25" parent="1" style="text;html=1;strokeColor=none;fillColor=#64748B;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="📋 条件" vertex="1">
<mxGeometry height="28" width="70" x="70" y="320" as="geometry" />
</mxCell>
<mxCell id="26" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=13;fontColor=#475569;fontStyle=1;" value="类中【没有】显式定义
任何构造方法" vertex="1">
<mxGeometry height="50" width="180" x="80" y="360" as="geometry" />
</mxCell>
<mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=32;fontColor=#3B82F6;fontStyle=1;" value="→" vertex="1">
<mxGeometry height="50" width="50" x="280" y="355" as="geometry" />
</mxCell>
<mxCell id="28" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#22C55E;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="120" width="200" x="330" y="320" as="geometry" />
</mxCell>
<mxCell id="29" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="⚡ 结果" vertex="1">
<mxGeometry height="28" width="70" x="330" y="320" as="geometry" />
</mxCell>
<mxCell id="30" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=13;fontColor=#166534;fontStyle=1;" value="编译器【自动】添加
一个无参构造方法" vertex="1">
<mxGeometry height="50" width="180" x="340" y="360" as="geometry" />
</mxCell>
<mxCell id="31" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="120" width="190" x="550" y="320" as="geometry" />
</mxCell>
<mxCell id="32" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="⚠️ 注意" vertex="1">
<mxGeometry height="28" width="70" x="550" y="320" as="geometry" />
</mxCell>
<mxCell id="33" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontColor=#92400E;fontStyle=1;" value="一旦定义了任何构造方法
默认构造就【不再】生成" vertex="1">
<mxGeometry height="50" width="180" x="555" y="360" as="geometry" />
</mxCell>
<mxCell id="34" parent="1" style="text;html=1;strokeColor=none;fillColor=#FDE68A;align=center;verticalAlign=middle;fontSize=11;fontColor=#92400E;rounded=1;fontStyle=2;" value="需要时请手动添加!" vertex="1">
<mxGeometry height="20" width="140" x="575" y="415" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
代码示例
// 源代码(没有写构造方法)
public class Product {
private String name;
private double price;
}
// 编译后等价于(编译器自动添加)
public class Product {
private String name;
private double price;
// 编译器添加的默认构造方法
public Product() {
// 空实现
}
}
// 可以正常使用无参构造
Product p = new Product(); // ✅ 正确
2.2 查看字节码验证
# 编译
javac Product.java
# 查看字节码
javap -c Product.class
输出结果:
public class Product {
public Product(); // ← 编译器生成的默认构造方法
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
}
2.3 默认构造方法的特点
| 特点 | 说明 |
|---|---|
| 访问修饰符 | 与类的访问修饰符相同 |
| 参数 | 无参数 |
| 方法体 | 空(仅调用父类无参构造) |
| 生成条件 | 类中没有任何显式构造方法 |
// 类是 public,默认构造也是 public
public class A {
}
// 类是默认访问权限,默认构造也是默认权限
class B {
}
三、手动定义构造方法
3.1 ⚠️ 重要规则
<mxfile>
<diagram id="core-constructor-rule" name="核心规则">
<mxGraphModel dx="850" dy="580" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="850" pageHeight="580">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="2" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#64748B;strokeWidth=2;" vertex="1" parent="1">
<mxGeometry x="20" y="20" width="810" height="540" as="geometry" />
</mxCell>
<mxCell id="3" value="⚠️ 核心规则" style="text;html=1;strokeColor=none;fillColor=#DC2626;align=center;verticalAlign=middle;fontSize=24;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
<mxGeometry x="20" y="20" width="810" height="55" as="geometry" />
</mxCell>
<mxCell id="4" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=3;shadow=1;" vertex="1" parent="1">
<mxGeometry x="50" y="95" width="320" height="180" as="geometry" />
</mxCell>
<mxCell id="5" value="📝 手动定义了【任何】构造方法" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
<mxGeometry x="50" y="95" width="320" height="35" as="geometry" />
</mxCell>
<mxCell id="6" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#475569;strokeWidth=2;" vertex="1" parent="1">
<mxGeometry x="70" y="145" width="280" height="115" as="geometry" />
</mxCell>
<mxCell id="7" value="public class Person {" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#E2E8F0;fontFamily=Consolas;" vertex="1" parent="1">
<mxGeometry x="85" y="152" width="250" height="18" as="geometry" />
</mxCell>
<mxCell id="8" value=" // 有参构造" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#6B7280;fontFamily=Consolas;fontStyle=2;" vertex="1" parent="1">
<mxGeometry x="85" y="172" width="250" height="18" as="geometry" />
</mxCell>
<mxCell id="9" value=" public Person(String n) {" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#FBBF24;fontFamily=Consolas;fontStyle=1;" vertex="1" parent="1">
<mxGeometry x="85" y="192" width="250" height="18" as="geometry" />
</mxCell>
<mxCell id="10" value=" this.name = n;" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#FBBF24;fontFamily=Consolas;fontStyle=1;" vertex="1" parent="1">
<mxGeometry x="85" y="212" width="250" height="18" as="geometry" />
</mxCell>
<mxCell id="11" value=" }" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#FBBF24;fontFamily=Consolas;fontStyle=1;" vertex="1" parent="1">
<mxGeometry x="85" y="232" width="250" height="18" as="geometry" />
</mxCell>
<mxCell id="12" value="}" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#E2E8F0;fontFamily=Consolas;" vertex="1" parent="1">
<mxGeometry x="85" y="250" width="250" height="18" as="geometry" />
</mxCell>
<mxCell id="13" value="" style="shape=flexArrow;endArrow=classic;html=1;strokeWidth=2;fillColor=#DC2626;strokeColor=#991B1B;width=25;endSize=10;" edge="1" parent="1">
<mxGeometry relative="1" as="geometry">
<mxPoint x="380" y="185" as="sourcePoint" />
<mxPoint x="480" y="185" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="14" value="导致" style="text;html=1;strokeColor=none;fillColor=#FEE2E2;align=center;verticalAlign=middle;fontSize=13;fontColor=#991B1B;rounded=1;fontStyle=1;" vertex="1" parent="1">
<mxGeometry x="400" y="155" width="50" height="24" as="geometry" />
</mxCell>
<mxCell id="15" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEE2E2;strokeColor=#EF4444;strokeWidth=3;shadow=1;" vertex="1" parent="1">
<mxGeometry x="480" y="95" width="330" height="180" as="geometry" />
</mxCell>
<mxCell id="16" value="🚫 编译器【不再】提供默认构造" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
<mxGeometry x="480" y="95" width="330" height="35" as="geometry" />
</mxCell>
<mxCell id="17" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#475569;strokeWidth=2;" vertex="1" parent="1">
<mxGeometry x="500" y="145" width="290" height="115" as="geometry" />
</mxCell>
<mxCell id="18" value="// 尝试使用无参构造" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#6B7280;fontFamily=Consolas;fontStyle=2;" vertex="1" parent="1">
<mxGeometry x="515" y="152" width="260" height="18" as="geometry" />
</mxCell>
<mxCell id="19" value="Person p = new Person();" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#F87171;fontFamily=Consolas;textDecoration=line-through;" vertex="1" parent="1">
<mxGeometry x="515" y="175" width="260" height="18" as="geometry" />
</mxCell>
<mxCell id="20" value="// ❌ 编译错误!" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#EF4444;fontFamily=Consolas;fontStyle=1;" vertex="1" parent="1">
<mxGeometry x="515" y="198" width="260" height="18" as="geometry" />
</mxCell>
<mxCell id="21" value="// 必须使用有参构造" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#6B7280;fontFamily=Consolas;fontStyle=2;" vertex="1" parent="1">
<mxGeometry x="515" y="221" width="260" height="18" as="geometry" />
</mxCell>
<mxCell id="22" value="Person p = new Person("Tom");" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#4ADE80;fontFamily=Consolas;fontStyle=1;" vertex="1" parent="1">
<mxGeometry x="515" y="241" width="260" height="18" as="geometry" />
</mxCell>
<mxCell id="23" value="✅" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;" vertex="1" parent="1">
<mxGeometry x="755" y="238" width="30" height="24" as="geometry" />
</mxCell>
<mxCell id="24" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEE2E2;strokeColor=#DC2626;strokeWidth=4;shadow=1;dashed=1;dashPattern=8 8;" vertex="1" parent="1">
<mxGeometry x="50" y="295" width="760" height="245" as="geometry" />
</mxCell>
<mxCell id="25" value="❌ 常见错误:定义了有参构造后,无参构造消失了" style="text;html=1;strokeColor=none;fillColor=#DC2626;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
<mxGeometry x="50" y="295" width="760" height="40" as="geometry" />
</mxCell>
<mxCell id="26" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#F87171;strokeWidth=2;" vertex="1" parent="1">
<mxGeometry x="70" y="350" width="340" height="170" as="geometry" />
</mxCell>
<mxCell id="27" value="😰 错误示例" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
<mxGeometry x="70" y="350" width="100" height="28" as="geometry" />
</mxCell>
<mxCell id="28" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#475569;strokeWidth=2;" vertex="1" parent="1">
<mxGeometry x="85" y="390" width="310" height="115" as="geometry" />
</mxCell>
<mxCell id="29" value="class Student {" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#E2E8F0;fontFamily=Consolas;" vertex="1" parent="1">
<mxGeometry x="100" y="395" width="280" height="16" as="geometry" />
</mxCell>
<mxCell id="30" value=" Student(String name) {...}" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#FBBF24;fontFamily=Consolas;" vertex="1" parent="1">
<mxGeometry x="100" y="413" width="280" height="16" as="geometry" />
</mxCell>
<mxCell id="31" value="}" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#E2E8F0;fontFamily=Consolas;" vertex="1" parent="1">
<mxGeometry x="100" y="431" width="280" height="16" as="geometry" />
</mxCell>
<mxCell id="32" value="// 调用" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#6B7280;fontFamily=Consolas;fontStyle=2;" vertex="1" parent="1">
<mxGeometry x="100" y="452" width="280" height="16" as="geometry" />
</mxCell>
<mxCell id="33" value="Student s = new Student();" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#F87171;fontFamily=Consolas;textDecoration=line-through;" vertex="1" parent="1">
<mxGeometry x="100" y="470" width="200" height="16" as="geometry" />
</mxCell>
<mxCell id="34" value="// 报错!" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#EF4444;fontFamily=Consolas;fontStyle=1;" vertex="1" parent="1">
<mxGeometry x="300" y="470" width="80" height="16" as="geometry" />
</mxCell>
<mxCell id="35" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#22C55E;strokeWidth=2;" vertex="1" parent="1">
<mxGeometry x="450" y="350" width="340" height="170" as="geometry" />
</mxCell>
<mxCell id="36" value="😊 正确做法" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
<mxGeometry x="450" y="350" width="100" height="28" as="geometry" />
</mxCell>
<mxCell id="37" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#475569;strokeWidth=2;" vertex="1" parent="1">
<mxGeometry x="465" y="390" width="310" height="115" as="geometry" />
</mxCell>
<mxCell id="38" value="class Student {" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#E2E8F0;fontFamily=Consolas;" vertex="1" parent="1">
<mxGeometry x="480" y="395" width="280" height="16" as="geometry" />
</mxCell>
<mxCell id="39" value=" Student() {}" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#4ADE80;fontFamily=Consolas;fontStyle=1;" vertex="1" parent="1">
<mxGeometry x="480" y="413" width="200" height="16" as="geometry" />
</mxCell>
<mxCell id="40" value="// 手动添加!" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=10;fontColor=#22C55E;fontFamily=Consolas;fontStyle=2;" vertex="1" parent="1">
<mxGeometry x="670" y="413" width="90" height="16" as="geometry" />
</mxCell>
<mxCell id="41" value=" Student(String name) {...}" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#FBBF24;fontFamily=Consolas;" vertex="1" parent="1">
<mxGeometry x="480" y="431" width="280" height="16" as="geometry" />
</mxCell>
<mxCell id="42" value="}" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#E2E8F0;fontFamily=Consolas;" vertex="1" parent="1">
<mxGeometry x="480" y="449" width="280" height="16" as="geometry" />
</mxCell>
<mxCell id="43" value="// 调用" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#6B7280;fontFamily=Consolas;fontStyle=2;" vertex="1" parent="1">
<mxGeometry x="480" y="470" width="280" height="16" as="geometry" />
</mxCell>
<mxCell id="44" value="Student s = new Student();" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#4ADE80;fontFamily=Consolas;fontStyle=1;" vertex="1" parent="1">
<mxGeometry x="480" y="488" width="200" height="16" as="geometry" />
</mxCell>
<mxCell id="45" value="// 成功!" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#22C55E;fontFamily=Consolas;fontStyle=1;" vertex="1" parent="1">
<mxGeometry x="680" y="488" width="80" height="16" as="geometry" />
</mxCell>
<mxCell id="46" value="→" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=36;fontColor=#22C55E;fontStyle=1;" vertex="1" parent="1">
<mxGeometry x="405" y="420" width="50" height="50" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
3.2 错误示例
public class Product {
private String name;
// 手动定义了有参构造
public Product(String name) {
this.name = name;
}
}
// 使用
public class Main {
public static void main(String[] args) {
Product p1 = new Product("手机"); // ✅ 正确
Product p2 = new Product(); // ❌ 编译错误!
}
}
编译错误信息:
Error: java: 无法将类 Product 中的构造器 Product 应用到给定类型
需要: java.lang.String
找到: 没有参数
原因: 实际参数列表和形式参数列表长度不同
3.3 解决方案:显式定义无参构造
public class Product {
private String name;
// ✅ 显式定义无参构造
public Product() {
this.name = "未命名商品";
}
// 有参构造
public Product(String name) {
this.name = name;
}
}
// 现在都可以使用
Product p1 = new Product(); // ✅ 调用无参构造
Product p2 = new Product("手机"); // ✅ 调用有参构造
3.4 流程图:默认构造的生成逻辑
<mxfile>
<diagram id="compiler-constructor-logic" name="编译器处理构造方法的逻辑">
<mxGraphModel dx="1281" dy="581" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="650" math="0" shadow="0">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="2" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#64748B;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="610" width="860" x="20" y="20" as="geometry" />
</mxCell>
<mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=22;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="🔧 编译器处理构造方法的逻辑" vertex="1">
<mxGeometry height="55" width="860" x="20" y="20" as="geometry" />
</mxCell>
<mxCell id="4" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#E0E7FF;strokeColor=#6366F1;strokeWidth=2;fontSize=14;fontStyle=1;fontColor=#4338CA;" value="开始编译" vertex="1">
<mxGeometry height="50" width="150" x="375" y="95" as="geometry" />
</mxCell>
<mxCell id="5" edge="1" parent="1" style="endArrow=classic;html=1;strokeWidth=2;strokeColor=#6366F1;" value="">
<mxGeometry relative="1" as="geometry">
<mxPoint x="450" y="145" as="sourcePoint" />
<mxPoint x="450" y="185" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="6" parent="1" style="rhombus;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=3;fontSize=14;fontStyle=1;fontColor=#92400E;" value="类中是否有
显式构造方法?" vertex="1">
<mxGeometry height="100" width="220" x="340" y="185" as="geometry" />
</mxCell>
<mxCell id="7" edge="1" parent="1" style="endArrow=classic;html=1;strokeWidth=3;strokeColor=#22C55E;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" target="11" value="">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="200" y="235" />
</Array>
<mxPoint x="340" y="235" as="sourcePoint" />
<mxPoint x="200" y="235" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="8" parent="1" style="text;html=1;strokeColor=none;fillColor=#DCFCE7;align=center;verticalAlign=middle;fontSize=14;fontColor=#166534;rounded=1;fontStyle=1;" value="NO" vertex="1">
<mxGeometry height="24" width="50" x="250" y="210" as="geometry" />
</mxCell>
<mxCell id="9" edge="1" parent="1" style="endArrow=classic;html=1;strokeWidth=3;strokeColor=#EF4444;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" target="13" value="">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="700" y="235" />
</Array>
<mxPoint x="560" y="235" as="sourcePoint" />
<mxPoint x="700" y="235" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="10" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEE2E2;align=center;verticalAlign=middle;fontSize=14;fontColor=#991B1B;rounded=1;fontStyle=1;" value="YES" vertex="1">
<mxGeometry height="24" width="50" x="600" y="210" as="geometry" />
</mxCell>
<mxCell id="11" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=16;fontColor=#FFFFFF;rounded=1;fontStyle=1;" value="没有" vertex="1">
<mxGeometry height="35" width="140" x="130" y="305" as="geometry" />
</mxCell>
<mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=16;fontColor=#FFFFFF;rounded=1;fontStyle=1;" value="有" vertex="1">
<mxGeometry height="35" width="140" x="630" y="305" as="geometry" />
</mxCell>
<mxCell id="15" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#22C55E;strokeWidth=3;shadow=1;" value="" vertex="1">
<mxGeometry height="255" width="300" x="50" y="355" as="geometry" />
</mxCell>
<mxCell id="16" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="✅ 自动生成默认构造方法" vertex="1">
<mxGeometry height="40" width="300" x="50" y="355" as="geometry" />
</mxCell>
<mxCell id="17" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#475569;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="100" width="260" x="70" y="410" as="geometry" />
</mxCell>
<mxCell id="18" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#6B7280;fontFamily=Consolas;fontStyle=2;" value="// 编译器自动添加" vertex="1">
<mxGeometry height="18" width="230" x="85" y="418" as="geometry" />
</mxCell>
<mxCell id="19" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#4ADE80;fontFamily=Consolas;fontStyle=1;" value="public 类名() {" vertex="1">
<mxGeometry height="20" width="230" x="85" y="440" as="geometry" />
</mxCell>
<mxCell id="20" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#4ADE80;fontFamily=Consolas;fontStyle=1;" value=" super();" vertex="1">
<mxGeometry height="20" width="230" x="85" y="462" as="geometry" />
</mxCell>
<mxCell id="21" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#4ADE80;fontFamily=Consolas;fontStyle=1;" value="}" vertex="1">
<mxGeometry height="20" width="230" x="85" y="484" as="geometry" />
</mxCell>
<mxCell id="22" parent="1" style="text;html=1;strokeColor=none;fillColor=#BBF7D0;align=center;verticalAlign=middle;fontSize=12;fontColor=#166534;rounded=1;fontStyle=1;" value="💡 可以使用 new 类名() 创建对象" vertex="1">
<mxGeometry height="30" width="260" x="70" y="525" as="geometry" />
</mxCell>
<mxCell id="23" parent="1" style="text;html=1;strokeColor=none;fillColor=#BBF7D0;align=center;verticalAlign=middle;fontSize=12;fontColor=#166534;rounded=1;" value="📌 访问修饰符与类一致" vertex="1">
<mxGeometry height="30" width="260" x="70" y="565" as="geometry" />
</mxCell>
<mxCell id="24" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEE2E2;strokeColor=#EF4444;strokeWidth=3;shadow=1;" value="" vertex="1">
<mxGeometry height="255" width="300" x="550" y="355" as="geometry" />
</mxCell>
<mxCell id="25" parent="1" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="🚫 不生成默认构造方法" vertex="1">
<mxGeometry height="40" width="300" x="550" y="355" as="geometry" />
</mxCell>
<mxCell id="26" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#475569;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="100" width="260" x="570" y="410" as="geometry" />
</mxCell>
<mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#6B7280;fontFamily=Consolas;fontStyle=2;" value="// 只能使用已定义的构造方法" vertex="1">
<mxGeometry height="18" width="230" x="585" y="418" as="geometry" />
</mxCell>
<mxCell id="28" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#FBBF24;fontFamily=Consolas;fontStyle=1;" value="public 类名(参数) {" vertex="1">
<mxGeometry height="20" width="230" x="585" y="440" as="geometry" />
</mxCell>
<mxCell id="29" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#94A3B8;fontFamily=Consolas;" value=" // 初始化代码" vertex="1">
<mxGeometry height="20" width="230" x="585" y="462" as="geometry" />
</mxCell>
<mxCell id="30" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#FBBF24;fontFamily=Consolas;fontStyle=1;" value="}" vertex="1">
<mxGeometry height="20" width="230" x="585" y="484" as="geometry" />
</mxCell>
<mxCell id="31" parent="1" style="text;html=1;strokeColor=none;fillColor=#FECACA;align=center;verticalAlign=middle;fontSize=12;fontColor=#991B1B;rounded=1;fontStyle=1;" value="⚠️ new 类名() 将编译失败" vertex="1">
<mxGeometry height="30" width="260" x="570" y="525" as="geometry" />
</mxCell>
<mxCell id="32" parent="1" style="text;html=1;strokeColor=none;fillColor=#FECACA;align=center;verticalAlign=middle;fontSize=12;fontColor=#991B1B;rounded=1;" value="📌 需要无参构造时必须手动添加" vertex="1">
<mxGeometry height="30" width="260" x="570" y="565" as="geometry" />
</mxCell>
<mxCell id="33" parent="1" style="shape=curlyBracket;whiteSpace=wrap;html=1;rounded=1;flipH=1;labelPosition=right;verticalLabelPosition=middle;align=left;verticalAlign=middle;strokeWidth=2;strokeColor=#6366F1;size=0.5;" value="" vertex="1">
<mxGeometry height="225" width="30" x="360" y="370" as="geometry" />
</mxCell>
<mxCell id="34" parent="1" style="shape=curlyBracket;whiteSpace=wrap;html=1;rounded=1;labelPosition=right;verticalLabelPosition=middle;align=left;verticalAlign=middle;strokeWidth=2;strokeColor=#6366F1;size=0.5;" value="" vertex="1">
<mxGeometry height="225" width="30" x="510" y="370" as="geometry" />
</mxCell>
<mxCell id="35" parent="1" style="text;html=1;strokeColor=none;fillColor=#E0E7FF;align=center;verticalAlign=middle;fontSize=13;fontColor=#4338CA;rounded=1;fontStyle=1;" value="编译器
决策" vertex="1">
<mxGeometry height="50" width="100" x="400" y="460" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
四、构造方法重载
4.1 什么是构造方法重载
<mxfile>
<diagram id="constructor-overload" name="构造方法重载">
<mxGraphModel dx="1281" dy="581" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="580" math="0" shadow="0">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="2" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#64748B;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="550" width="860" x="20" y="20" as="geometry" />
</mxCell>
<mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=24;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="🔄 构造方法重载" vertex="1">
<mxGeometry height="55" width="860" x="20" y="20" as="geometry" />
</mxCell>
<mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F3E8FF;strokeColor=#A855F7;strokeWidth=2;shadow=1;" value="" vertex="1">
<mxGeometry height="60" width="820" x="40" y="90" as="geometry" />
</mxCell>
<mxCell id="5" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=16;fontColor=#6B21A8;fontStyle=1;" value="💡 同一个类中可以定义多个构造方法,只要它们的【参数列表不同】" vertex="1">
<mxGeometry height="60" width="820" x="40" y="90" as="geometry" />
</mxCell>
<mxCell id="6" parent="1" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="参数列表不同包括" vertex="1">
<mxGeometry height="35" width="200" x="350" y="165" as="geometry" />
</mxCell>
<mxCell id="7" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#3B82F6;strokeWidth=3;shadow=1;" value="" vertex="1">
<mxGeometry height="200" width="260" x="40" y="215" as="geometry" />
</mxCell>
<mxCell id="8" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="1️⃣ 参数个数不同" vertex="1">
<mxGeometry height="35" width="260" x="40" y="215" as="geometry" />
</mxCell>
<mxCell id="9" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#475569;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="145" width="230" x="55" y="260" as="geometry" />
</mxCell>
<mxCell id="10" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#6B7280;fontFamily=Consolas;fontStyle=2;" value="// 无参" vertex="1">
<mxGeometry height="16" width="210" x="65" y="268" as="geometry" />
</mxCell>
<mxCell id="11" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#60A5FA;fontFamily=Consolas;fontStyle=1;" value="Person() {}" vertex="1">
<mxGeometry height="16" width="210" x="65" y="286" as="geometry" />
</mxCell>
<mxCell id="12" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#6B7280;fontFamily=Consolas;fontStyle=2;" value="// 1个参数" vertex="1">
<mxGeometry height="16" width="210" x="65" y="308" as="geometry" />
</mxCell>
<mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#60A5FA;fontFamily=Consolas;fontStyle=1;" value="Person(String name) {}" vertex="1">
<mxGeometry height="16" width="210" x="65" y="326" as="geometry" />
</mxCell>
<mxCell id="14" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#6B7280;fontFamily=Consolas;fontStyle=2;" value="// 2个参数" vertex="1">
<mxGeometry height="16" width="210" x="65" y="348" as="geometry" />
</mxCell>
<mxCell id="15" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#60A5FA;fontFamily=Consolas;fontStyle=1;" value="Person(String name, int age) {}" vertex="1">
<mxGeometry height="16" width="210" x="65" y="366" as="geometry" />
</mxCell>
<mxCell id="16" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=20;" value="✅" vertex="1">
<mxGeometry height="24" width="30" x="250" y="378" as="geometry" />
</mxCell>
<mxCell id="17" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#D1FAE5;strokeColor=#10B981;strokeWidth=3;shadow=1;" value="" vertex="1">
<mxGeometry height="200" width="260" x="320" y="215" as="geometry" />
</mxCell>
<mxCell id="18" parent="1" style="text;html=1;strokeColor=none;fillColor=#10B981;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="2️⃣ 参数类型不同" vertex="1">
<mxGeometry height="35" width="260" x="320" y="215" as="geometry" />
</mxCell>
<mxCell id="19" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#475569;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="145" width="230" x="335" y="260" as="geometry" />
</mxCell>
<mxCell id="20" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#6B7280;fontFamily=Consolas;fontStyle=2;" value="// String 类型" vertex="1">
<mxGeometry height="16" width="210" x="345" y="268" as="geometry" />
</mxCell>
<mxCell id="21" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#4ADE80;fontFamily=Consolas;fontStyle=1;" value="Person(String name) {}" vertex="1">
<mxGeometry height="16" width="210" x="345" y="286" as="geometry" />
</mxCell>
<mxCell id="22" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#6B7280;fontFamily=Consolas;fontStyle=2;" value="// int 类型" vertex="1">
<mxGeometry height="16" width="210" x="345" y="308" as="geometry" />
</mxCell>
<mxCell id="23" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#4ADE80;fontFamily=Consolas;fontStyle=1;" value="Person(int age) {}" vertex="1">
<mxGeometry height="16" width="210" x="345" y="326" as="geometry" />
</mxCell>
<mxCell id="24" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#6B7280;fontFamily=Consolas;fontStyle=2;" value="// double 类型" vertex="1">
<mxGeometry height="16" width="210" x="345" y="348" as="geometry" />
</mxCell>
<mxCell id="25" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#4ADE80;fontFamily=Consolas;fontStyle=1;" value="Person(double salary) {}" vertex="1">
<mxGeometry height="16" width="210" x="345" y="366" as="geometry" />
</mxCell>
<mxCell id="26" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=20;" value="✅" vertex="1">
<mxGeometry height="24" width="30" x="530" y="378" as="geometry" />
</mxCell>
<mxCell id="27" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=3;shadow=1;" value="" vertex="1">
<mxGeometry height="200" width="260" x="600" y="215" as="geometry" />
</mxCell>
<mxCell id="28" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="3️⃣ 参数顺序不同" vertex="1">
<mxGeometry height="35" width="260" x="600" y="215" as="geometry" />
</mxCell>
<mxCell id="29" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#475569;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="105" width="230" x="615" y="260" as="geometry" />
</mxCell>
<mxCell id="30" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#6B7280;fontFamily=Consolas;fontStyle=2;" value="// String, int 顺序" vertex="1">
<mxGeometry height="16" width="210" x="625" y="268" as="geometry" />
</mxCell>
<mxCell id="31" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#FBBF24;fontFamily=Consolas;fontStyle=1;" value="Person(String s, int i) {}" vertex="1">
<mxGeometry height="16" width="210" x="625" y="286" as="geometry" />
</mxCell>
<mxCell id="32" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#6B7280;fontFamily=Consolas;fontStyle=2;" value="// int, String 顺序" vertex="1">
<mxGeometry height="16" width="210" x="625" y="308" as="geometry" />
</mxCell>
<mxCell id="33" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#FBBF24;fontFamily=Consolas;fontStyle=1;" value="Person(int i, String s) {}" vertex="1">
<mxGeometry height="16" width="210" x="625" y="326" as="geometry" />
</mxCell>
<mxCell id="34" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEE2E2;align=center;verticalAlign=middle;fontSize=12;fontColor=#991B1B;rounded=1;fontStyle=1;" value="⚠️ 不推荐依赖此方式" vertex="1">
<mxGeometry height="30" width="230" x="615" y="375" as="geometry" />
</mxCell>
<mxCell id="35" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#8B5CF6;strokeWidth=3;shadow=1;" value="" vertex="1">
<mxGeometry height="120" width="820" x="40" y="430" as="geometry" />
</mxCell>
<mxCell id="36" parent="1" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="📝 完整示例" vertex="1">
<mxGeometry height="30" width="120" x="40" y="430" as="geometry" />
</mxCell>
<mxCell id="37" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#E2E8F0;fontFamily=Consolas;" value="public class Person {" vertex="1">
<mxGeometry height="18" width="200" x="60" y="468" as="geometry" />
</mxCell>
<mxCell id="38" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#60A5FA;fontFamily=Consolas;fontStyle=1;" value="public Person() { }" vertex="1">
<mxGeometry height="18" width="200" x="80" y="488" as="geometry" />
</mxCell>
<mxCell id="39" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#6B7280;fontFamily=Consolas;fontStyle=2;" value="// 无参构造" vertex="1">
<mxGeometry height="18" width="100" x="260" y="488" as="geometry" />
</mxCell>
<mxCell id="40" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#4ADE80;fontFamily=Consolas;fontStyle=1;" value="public Person(String name) { this.name = name; }" vertex="1">
<mxGeometry height="18" width="400" x="370" y="468" as="geometry" />
</mxCell>
<mxCell id="41" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#6B7280;fontFamily=Consolas;fontStyle=2;" value="// 1参构造" vertex="1">
<mxGeometry height="18" width="90" x="760" y="468" as="geometry" />
</mxCell>
<mxCell id="42" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#FBBF24;fontFamily=Consolas;fontStyle=1;" value="public Person(String name, int age) { this.name = name; this.age = age; }" vertex="1">
<mxGeometry height="18" width="480" x="370" y="488" as="geometry" />
</mxCell>
<mxCell id="43" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#6B7280;fontFamily=Consolas;fontStyle=2;" value="// 2参构造" vertex="1">
<mxGeometry height="18" width="90" x="760" y="508" as="geometry" />
</mxCell>
<mxCell id="44" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#E2E8F0;fontFamily=Consolas;" value="}" vertex="1">
<mxGeometry height="18" width="200" x="60" y="520" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
4.2 代码示例
public class Student {
private String name;
private int age;
private String school;
// 构造方法 1:无参
public Student() {
this.name = "未知";
this.age = 0;
this.school = "未知";
}
// 构造方法 2:一个参数
public Student(String name) {
this.name = name;
this.age = 0;
this.school = "未知";
}
// 构造方法 3:两个参数
public Student(String name, int age) {
this.name = name;
this.age = age;
this.school = "未知";
}
// 构造方法 4:三个参数(全参构造)
public Student(String name, int age, String school) {
this.name = name;
this.age = age;
this.school = school;
}
}
// 多种创建方式
Student s1 = new Student(); // 调用构造1
Student s2 = new Student("张三"); // 调用构造2
Student s3 = new Student("李四", 20); // 调用构造3
Student s4 = new Student("王五", 21, "清华大学"); // 调用构造4
4.3 使用 this() 调用其他构造方法
public class Student {
private String name;
private int age;
private String school;
// 全参构造(主构造方法)
public Student(String name, int age, String school) {
this.name = name;
this.age = age;
this.school = school;
System.out.println("创建学生:" + name);
}
// 无参构造 → 调用全参构造
public Student() {
this("未知", 0, "未知"); // ✅ 调用全参构造
}
// 一参构造 → 调用全参构造
public Student(String name) {
this(name, 0, "未知"); // ✅ 调用全参构造
}
// 两参构造 → 调用全参构造
public Student(String name, int age) {
this(name, age, "未知"); // ✅ 调用全参构造
}
}
调用链示意图:
<mxfile>
<diagram id="this-constructor-chain" name="this() 构造方法调用链">
<mxGraphModel dx="1281" dy="581" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="620" math="0" shadow="0">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="2" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#64748B;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="600" width="860" x="10" y="10" as="geometry" />
</mxCell>
<mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#0EA5E9;align=center;verticalAlign=middle;fontSize=24;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="🔗 this() 构造方法调用链" vertex="1">
<mxGeometry height="55" width="860" x="10" y="10" as="geometry" />
</mxCell>
<mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#E0F2FE;strokeColor=#0EA5E9;strokeWidth=2;dashed=1;" value="" vertex="1">
<mxGeometry height="380" width="280" x="40" y="90" as="geometry" />
</mxCell>
<mxCell id="5" parent="1" style="text;html=1;strokeColor=none;fillColor=#0EA5E9;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="📥 入口构造方法" vertex="1">
<mxGeometry height="35" width="280" x="40" y="90" as="geometry" />
</mxCell>
<mxCell id="6" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#3B82F6;strokeWidth=3;shadow=1;" value="" vertex="1">
<mxGeometry height="95" width="240" x="60" y="140" as="geometry" />
</mxCell>
<mxCell id="7" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="Student()" vertex="1">
<mxGeometry height="28" width="240" x="60" y="140" as="geometry" />
</mxCell>
<mxCell id="8" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontColor=#1E40AF;fontStyle=1;" value="无参构造" vertex="1">
<mxGeometry height="20" width="240" x="60" y="170" as="geometry" />
</mxCell>
<mxCell id="9" parent="1" style="text;html=1;strokeColor=none;fillColor=#1E293B;align=center;verticalAlign=middle;fontSize=11;fontColor=#60A5FA;fontFamily=Consolas;rounded=1;" value="this("默认", 0, "未知");" vertex="1">
<mxGeometry height="30" width="220" x="70" y="195" as="geometry" />
</mxCell>
<mxCell id="10" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#D1FAE5;strokeColor=#10B981;strokeWidth=3;shadow=1;" value="" vertex="1">
<mxGeometry height="95" width="240" x="60" y="250" as="geometry" />
</mxCell>
<mxCell id="11" parent="1" style="text;html=1;strokeColor=none;fillColor=#10B981;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="Student(String name)" vertex="1">
<mxGeometry height="28" width="240" x="60" y="250" as="geometry" />
</mxCell>
<mxCell id="12" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontColor=#047857;fontStyle=1;" value="一参构造" vertex="1">
<mxGeometry height="20" width="240" x="60" y="280" as="geometry" />
</mxCell>
<mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=#1E293B;align=center;verticalAlign=middle;fontSize=11;fontColor=#4ADE80;fontFamily=Consolas;rounded=1;" value="this(name, 18, "默认学校");" vertex="1">
<mxGeometry height="30" width="220" x="70" y="305" as="geometry" />
</mxCell>
<mxCell id="14" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=3;shadow=1;" value="" vertex="1">
<mxGeometry height="95" width="240" x="60" y="360" as="geometry" />
</mxCell>
<mxCell id="15" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="Student(String name, int age)" vertex="1">
<mxGeometry height="28" width="240" x="60" y="360" as="geometry" />
</mxCell>
<mxCell id="16" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontColor=#B45309;fontStyle=1;" value="两参构造" vertex="1">
<mxGeometry height="20" width="240" x="60" y="390" as="geometry" />
</mxCell>
<mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=#1E293B;align=center;verticalAlign=middle;fontSize=11;fontColor=#FBBF24;fontFamily=Consolas;rounded=1;" value="this(name, age, "默认学校");" vertex="1">
<mxGeometry height="30" width="220" x="70" y="415" as="geometry" />
</mxCell>
<mxCell id="18" edge="1" parent="1" style="endArrow=classic;html=1;strokeWidth=3;strokeColor=#3B82F6;entryX=0;entryY=0.25;entryDx=0;entryDy=0;curved=1;" value="">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="400" y="160" />
</Array>
<mxPoint x="300" y="187" as="sourcePoint" />
<mxPoint x="520" y="187" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="19" edge="1" parent="1" style="endArrow=classic;html=1;strokeWidth=3;strokeColor=#10B981;entryX=0;entryY=0.5;entryDx=0;entryDy=0;" value="">
<mxGeometry relative="1" as="geometry">
<mxPoint x="300" y="297" as="sourcePoint" />
<mxPoint x="520" y="297" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="20" edge="1" parent="1" style="endArrow=classic;html=1;strokeWidth=3;strokeColor=#F59E0B;entryX=0;entryY=0.75;entryDx=0;entryDy=0;curved=1;" value="">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="400" y="440" />
</Array>
<mxPoint x="300" y="407" as="sourcePoint" />
<mxPoint x="520" y="407" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="21" parent="1" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=center;verticalAlign=middle;fontSize=11;fontColor=#1E40AF;rounded=1;fontStyle=1;" value="this()" vertex="1">
<mxGeometry height="22" width="50" x="380" y="135" as="geometry" />
</mxCell>
<mxCell id="22" parent="1" style="text;html=1;strokeColor=none;fillColor=#D1FAE5;align=center;verticalAlign=middle;fontSize=11;fontColor=#047857;rounded=1;fontStyle=1;" value="this()" vertex="1">
<mxGeometry height="22" width="50" x="380" y="275" as="geometry" />
</mxCell>
<mxCell id="23" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEF3C7;align=center;verticalAlign=middle;fontSize=11;fontColor=#B45309;rounded=1;fontStyle=1;" value="this()" vertex="1">
<mxGeometry height="22" width="50" x="380" y="445" as="geometry" />
</mxCell>
<mxCell id="24" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEE2E2;strokeColor=#EF4444;strokeWidth=4;shadow=1;" value="" vertex="1">
<mxGeometry height="330" width="320" x="520" y="115" as="geometry" />
</mxCell>
<mxCell id="25" parent="1" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="🎯 全参构造(真正执行初始化)" vertex="1">
<mxGeometry height="40" width="320" x="520" y="115" as="geometry" />
</mxCell>
<mxCell id="26" parent="1" style="text;html=1;strokeColor=none;fillColor=#1E293B;align=center;verticalAlign=middle;fontSize=12;fontColor=#F87171;fontFamily=Consolas;rounded=1;fontStyle=1;" value="Student(String name, int age, String school)" vertex="1">
<mxGeometry height="30" width="290" x="535" y="170" as="geometry" />
</mxCell>
<mxCell id="27" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#475569;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="130" width="290" x="535" y="215" as="geometry" />
</mxCell>
<mxCell id="28" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#6B7280;fontFamily=Consolas;fontStyle=2;" value="// 所有初始化逻辑集中在这里" vertex="1">
<mxGeometry height="18" width="260" x="550" y="225" as="geometry" />
</mxCell>
<mxCell id="29" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#F87171;fontFamily=Consolas;fontStyle=1;" value="this.name = name;" vertex="1">
<mxGeometry height="22" width="260" x="550" y="250" as="geometry" />
</mxCell>
<mxCell id="30" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#F87171;fontFamily=Consolas;fontStyle=1;" value="this.age = age;" vertex="1">
<mxGeometry height="22" width="260" x="550" y="275" as="geometry" />
</mxCell>
<mxCell id="31" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#F87171;fontFamily=Consolas;fontStyle=1;" value="this.school = school;" vertex="1">
<mxGeometry height="22" width="260" x="550" y="300" as="geometry" />
</mxCell>
<mxCell id="32" parent="1" style="text;html=1;strokeColor=none;fillColor=#BBF7D0;align=center;verticalAlign=middle;fontSize=12;fontColor=#166534;rounded=1;fontStyle=1;" value="✅ 统一初始化入口" vertex="1">
<mxGeometry height="30" width="140" x="535" y="360" as="geometry" />
</mxCell>
<mxCell id="33" parent="1" style="text;html=1;strokeColor=none;fillColor=#BBF7D0;align=center;verticalAlign=middle;fontSize=12;fontColor=#166534;rounded=1;fontStyle=1;" value="✅ 便于维护修改" vertex="1">
<mxGeometry height="30" width="140" x="685" y="360" as="geometry" />
</mxCell>
<mxCell id="34" parent="1" style="text;html=1;strokeColor=none;fillColor=#BBF7D0;align=center;verticalAlign=middle;fontSize=12;fontColor=#166534;rounded=1;fontStyle=1;" value="✅ 避免代码重复" vertex="1">
<mxGeometry height="30" width="140" x="535" y="400" as="geometry" />
</mxCell>
<mxCell id="35" parent="1" style="text;html=1;strokeColor=none;fillColor=#BBF7D0;align=center;verticalAlign=middle;fontSize=12;fontColor=#166534;rounded=1;fontStyle=1;" value="✅ 逻辑一致性" vertex="1">
<mxGeometry height="30" width="140" x="685" y="400" as="geometry" />
</mxCell>
<mxCell id="36" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F3E8FF;strokeColor=#A855F7;strokeWidth=3;shadow=1;" value="" vertex="1">
<mxGeometry height="100" width="800" x="40" y="485" as="geometry" />
</mxCell>
<mxCell id="37" parent="1" style="text;html=1;strokeColor=none;fillColor=#A855F7;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="💡 设计模式优点" vertex="1">
<mxGeometry height="30" width="150" x="40" y="485" as="geometry" />
</mxCell>
<mxCell id="38" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#6B21A8;fontStyle=0;" value="🔹 所有构造方法最终汇聚到全参构造,形成调用链" vertex="1">
<mxGeometry height="25" width="380" x="60" y="520" as="geometry" />
</mxCell>
<mxCell id="39" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#6B21A8;fontStyle=0;" value="🔹 修改初始化逻辑只需改一处,降低维护成本" vertex="1">
<mxGeometry height="25" width="380" x="60" y="550" as="geometry" />
</mxCell>
<mxCell id="40" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#6B21A8;fontStyle=0;" value="🔹 this() 必须是构造方法的第一条语句" vertex="1">
<mxGeometry height="25" width="380" x="450" y="520" as="geometry" />
</mxCell>
<mxCell id="41" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#6B21A8;fontStyle=0;" value="🔹 避免循环调用:A→B→A 会编译错误" vertex="1">
<mxGeometry height="25" width="380" x="450" y="550" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
4.4 this() 使用规则
| 规则 | 说明 |
|---|---|
| 必须在第一行 | this() 必须是构造方法的第一条语句 |
| 只能调用一次 | 一个构造方法中只能有一个 this() |
| 不能循环调用 | A 调 B,B 不能再调 A |
| 不能和 super() 共存 | 二者都要求在第一行 |
// ❌ 错误示例
public Student() {
System.out.println("准备创建"); // 错误!this() 必须在第一行
this("默认");
}
// ❌ 错误示例:循环调用
public Student() {
this("默认"); // 调用一参构造
}
public Student(String name) {
this(); // 又调用无参构造 → 死循环!编译器报错
}
五、构造方法的特点总结
5.1 五大特点一览表
| 序号 | 特点 | 说明 | 示例 |
|---|---|---|---|
| 1️⃣ | 方法名与类名相同 | 严格一致,包括大小写 | class Dog { Dog() {} } |
| 2️⃣ | 没有返回值类型 | 连 void 都不写 | public Student() {} |
| 3️⃣ | 自动调用 | new 时自动执行 | new Student() |
| 4️⃣ | 可以重载 | 多个构造,参数不同 | 见上文示例 |
| 5️⃣ | 不能被修饰 | 不能用 static/final/abstract | — |
5.2 特点详解图
<mxfile>
<diagram id="constructor-features" name="构造方法五大特点">
<mxGraphModel dx="1281" dy="581" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="1050" math="0" shadow="0">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="2" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#64748B;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="1030" width="860" x="20" y="20" as="geometry" />
</mxCell>
<mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=24;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="🏗️ 构造方法五大特点" vertex="1">
<mxGeometry height="55" width="860" x="20" y="20" as="geometry" />
</mxCell>
<mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#3B82F6;strokeWidth=3;shadow=1;" value="" vertex="1">
<mxGeometry height="160" width="820" x="40" y="90" as="geometry" />
</mxCell>
<mxCell id="5" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="1️⃣ 方法名与类名相同" vertex="1">
<mxGeometry height="35" width="820" x="40" y="90" as="geometry" />
</mxCell>
<mxCell id="6" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#475569;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="100" width="780" x="60" y="135" as="geometry" />
</mxCell>
<mxCell id="7" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#E2E8F0;fontFamily=Consolas;" value="public class Student {" vertex="1">
<mxGeometry height="20" width="300" x="80" y="145" as="geometry" />
</mxCell>
<mxCell id="8" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#4ADE80;fontFamily=Consolas;fontStyle=1;" value="public Student() { }" vertex="1">
<mxGeometry height="20" width="200" x="100" y="170" as="geometry" />
</mxCell>
<mxCell id="9" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#4ADE80;fontFamily=Consolas;" value="// ✅ 方法名 = 类名" vertex="1">
<mxGeometry height="20" width="180" x="310" y="170" as="geometry" />
</mxCell>
<mxCell id="10" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#F87171;fontFamily=Consolas;fontStyle=1;" value="public student() { }" vertex="1">
<mxGeometry height="20" width="200" x="100" y="195" as="geometry" />
</mxCell>
<mxCell id="11" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#F87171;fontFamily=Consolas;" value="// ❌ 小写不行" vertex="1">
<mxGeometry height="20" width="180" x="310" y="195" as="geometry" />
</mxCell>
<mxCell id="12" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#E2E8F0;fontFamily=Consolas;" value="}" vertex="1">
<mxGeometry height="20" width="50" x="80" y="215" as="geometry" />
</mxCell>
<mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=center;verticalAlign=middle;fontSize=12;fontColor=#1E40AF;rounded=1;fontStyle=1;" value="💡 大小写敏感,必须完全一致" vertex="1">
<mxGeometry height="30" width="270" x="550" y="175" as="geometry" />
</mxCell>
<mxCell id="14" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#D1FAE5;strokeColor=#10B981;strokeWidth=3;shadow=1;" value="" vertex="1">
<mxGeometry height="160" width="820" x="40" y="265" as="geometry" />
</mxCell>
<mxCell id="15" parent="1" style="text;html=1;strokeColor=none;fillColor=#10B981;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="2️⃣ 没有返回值类型(连 void 都没有)" vertex="1">
<mxGeometry height="35" width="820" x="40" y="265" as="geometry" />
</mxCell>
<mxCell id="16" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#475569;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="100" width="780" x="60" y="310" as="geometry" />
</mxCell>
<mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#4ADE80;fontFamily=Consolas;fontStyle=1;" value="public Student() { }" vertex="1">
<mxGeometry height="22" width="250" x="80" y="320" as="geometry" />
</mxCell>
<mxCell id="18" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#4ADE80;fontFamily=Consolas;" value="// ✅ 正确 - 无返回值类型" vertex="1">
<mxGeometry height="22" width="200" x="340" y="320" as="geometry" />
</mxCell>
<mxCell id="19" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#F87171;fontFamily=Consolas;fontStyle=1;" value="public void Student() { }" vertex="1">
<mxGeometry height="22" width="250" x="80" y="350" as="geometry" />
</mxCell>
<mxCell id="20" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#F87171;fontFamily=Consolas;" value="// ❌ 这是普通方法!不是构造方法" vertex="1">
<mxGeometry height="22" width="260" x="340" y="350" as="geometry" />
</mxCell>
<mxCell id="21" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#F87171;fontFamily=Consolas;fontStyle=1;" value="public Student Student() { }" vertex="1">
<mxGeometry height="22" width="250" x="80" y="380" as="geometry" />
</mxCell>
<mxCell id="22" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#F87171;fontFamily=Consolas;" value="// ❌ 语法错误" vertex="1">
<mxGeometry height="22" width="200" x="340" y="380" as="geometry" />
</mxCell>
<mxCell id="23" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEE2E2;align=center;verticalAlign=middle;fontSize=12;fontColor=#991B1B;rounded=1;fontStyle=1;" value="⚠️ 加了 void 就变成普通方法了" vertex="1">
<mxGeometry height="30" width="210" x="610" y="345" as="geometry" />
</mxCell>
<mxCell id="24" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=3;shadow=1;" value="" vertex="1">
<mxGeometry height="145" width="820" x="40" y="440" as="geometry" />
</mxCell>
<mxCell id="25" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="3️⃣ 对象创建时自动调用" vertex="1">
<mxGeometry height="35" width="820" x="40" y="440" as="geometry" />
</mxCell>
<mxCell id="26" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#475569;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="85" width="400" x="60" y="485" as="geometry" />
</mxCell>
<mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#FBBF24;fontFamily=Consolas;fontStyle=1;" value="Student stu = new Student();" vertex="1">
<mxGeometry height="25" width="350" x="80" y="500" as="geometry" />
</mxCell>
<mxCell id="28" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=18;fontColor=#FBBF24;fontStyle=1;" value="↑" vertex="1">
<mxGeometry height="20" width="30" x="280" y="525" as="geometry" />
</mxCell>
<mxCell id="29" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#94A3B8;fontFamily=Consolas;" value="自动调用 Student()" vertex="1">
<mxGeometry height="18" width="150" x="230" y="545" as="geometry" />
</mxCell>
<mxCell id="30" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#D97706;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="85" width="360" x="480" y="485" as="geometry" />
</mxCell>
<mxCell id="31" parent="1" style="text;html=1;strokeColor=none;fillColor=#D97706;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="🔄 执行顺序" vertex="1">
<mxGeometry height="25" width="100" x="480" y="485" as="geometry" />
</mxCell>
<mxCell id="32" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#92400E;" value="1. 分配内存空间" vertex="1">
<mxGeometry height="20" width="150" x="500" y="515" as="geometry" />
</mxCell>
<mxCell id="33" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#92400E;" value="2. 执行构造方法" vertex="1">
<mxGeometry height="20" width="150" x="500" y="538" as="geometry" />
</mxCell>
<mxCell id="34" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#92400E;" value="3. 返回对象引用" vertex="1">
<mxGeometry height="20" width="150" x="660" y="515" as="geometry" />
</mxCell>
<mxCell id="35" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FCE7F3;strokeColor=#EC4899;strokeWidth=3;shadow=1;" value="" vertex="1">
<mxGeometry height="145" width="820" x="40" y="600" as="geometry" />
</mxCell>
<mxCell id="36" parent="1" style="text;html=1;strokeColor=none;fillColor=#EC4899;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="4️⃣ 可以重载,不能重写" vertex="1">
<mxGeometry height="35" width="820" x="40" y="600" as="geometry" />
</mxCell>
<mxCell id="37" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#D1FAE5;strokeColor=#10B981;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="85" width="380" x="60" y="645" as="geometry" />
</mxCell>
<mxCell id="38" parent="1" style="text;html=1;strokeColor=none;fillColor=#10B981;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="✅ 重载 Overload" vertex="1">
<mxGeometry height="28" width="140" x="60" y="645" as="geometry" />
</mxCell>
<mxCell id="39" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#047857;fontStyle=0;" value="同一个类中可以有多个构造方法" vertex="1">
<mxGeometry height="20" width="250" x="75" y="680" as="geometry" />
</mxCell>
<mxCell id="40" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#047857;fontStyle=0;" value="参数列表不同即可区分" vertex="1">
<mxGeometry height="20" width="250" x="75" y="702" as="geometry" />
</mxCell>
<mxCell id="41" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEE2E2;strokeColor=#EF4444;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="85" width="380" x="460" y="645" as="geometry" />
</mxCell>
<mxCell id="42" parent="1" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="❌ 重写 Override" vertex="1">
<mxGeometry height="28" width="140" x="460" y="645" as="geometry" />
</mxCell>
<mxCell id="43" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#991B1B;fontStyle=0;" value="构造方法不能被继承" vertex="1">
<mxGeometry height="20" width="250" x="475" y="680" as="geometry" />
</mxCell>
<mxCell id="44" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#991B1B;fontStyle=0;" value="所以子类无法重写父类构造方法" vertex="1">
<mxGeometry height="20" width="250" x="475" y="702" as="geometry" />
</mxCell>
<mxCell id="45" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#E0E7FF;strokeColor=#6366F1;strokeWidth=3;shadow=1;" value="" vertex="1">
<mxGeometry height="255" width="820" x="40" y="760" as="geometry" />
</mxCell>
<mxCell id="46" parent="1" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="5️⃣ 不能被 static / final / abstract 修饰" vertex="1">
<mxGeometry height="35" width="820" x="40" y="760" as="geometry" />
</mxCell>
<mxCell id="47" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEE2E2;strokeColor=#EF4444;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="90" width="250" x="60" y="810" as="geometry" />
</mxCell>
<mxCell id="48" parent="1" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="❌ static" vertex="1">
<mxGeometry height="28" width="250" x="60" y="810" as="geometry" />
</mxCell>
<mxCell id="49" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontColor=#991B1B;" value="构造方法用于创建对象实例" vertex="1">
<mxGeometry height="20" width="250" x="60" y="845" as="geometry" />
</mxCell>
<mxCell id="50" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontColor=#991B1B;" value="必须与具体对象关联" vertex="1">
<mxGeometry height="20" width="250" x="60" y="865" as="geometry" />
</mxCell>
<mxCell id="51" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontColor=#991B1B;fontStyle=1;" value="static 是类级别,矛盾!" vertex="1">
<mxGeometry height="20" width="250" x="60" y="885" as="geometry" />
</mxCell>
<mxCell id="52" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="90" width="250" x="325" y="810" as="geometry" />
</mxCell>
<mxCell id="53" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="❌ final" vertex="1">
<mxGeometry height="28" width="250" x="325" y="810" as="geometry" />
</mxCell>
<mxCell id="54" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontColor=#92400E;" value="构造方法本身不能被继承" vertex="1">
<mxGeometry height="20" width="250" x="325" y="845" as="geometry" />
</mxCell>
<mxCell id="55" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontColor=#92400E;" value="所以无需用 final 防止重写" vertex="1">
<mxGeometry height="20" width="250" x="325" y="865" as="geometry" />
</mxCell>
<mxCell id="56" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontColor=#92400E;fontStyle=1;" value="final 修饰没有意义" vertex="1">
<mxGeometry height="20" width="250" x="325" y="885" as="geometry" />
</mxCell>
<mxCell id="57" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#E0E7FF;strokeColor=#6366F1;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="90" width="250" x="590" y="810" as="geometry" />
</mxCell>
<mxCell id="58" parent="1" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="❌ abstract" vertex="1">
<mxGeometry height="28" width="250" x="590" y="810" as="geometry" />
</mxCell>
<mxCell id="59" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontColor=#4338CA;" value="构造方法必须有具体实现" vertex="1">
<mxGeometry height="20" width="250" x="590" y="845" as="geometry" />
</mxCell>
<mxCell id="60" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontColor=#4338CA;" value="用于初始化对象状态" vertex="1">
<mxGeometry height="20" width="250" x="590" y="865" as="geometry" />
</mxCell>
<mxCell id="61" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontColor=#4338CA;fontStyle=1;" value="abstract 无实现,矛盾!" vertex="1">
<mxGeometry height="20" width="250" x="590" y="885" as="geometry" />
</mxCell>
<mxCell id="62" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#475569;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="85" width="780" x="60" y="915" as="geometry" />
</mxCell>
<mxCell id="63" parent="1" style="text;html=1;strokeColor=none;fillColor=#10B981;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="📝 允许的修饰符" vertex="1">
<mxGeometry height="25" width="120" x="60" y="915" as="geometry" />
</mxCell>
<mxCell id="64" parent="1" style="text;html=1;strokeColor=none;fillColor=#4ADE80;align=center;verticalAlign=middle;fontSize=12;fontColor=#1E293B;rounded=1;fontStyle=1;" value="public" vertex="1">
<mxGeometry height="30" width="80" x="80" y="950" as="geometry" />
</mxCell>
<mxCell id="65" parent="1" style="text;html=1;strokeColor=none;fillColor=#60A5FA;align=center;verticalAlign=middle;fontSize=12;fontColor=#1E293B;rounded=1;fontStyle=1;" value="protected" vertex="1">
<mxGeometry height="30" width="80" x="175" y="950" as="geometry" />
</mxCell>
<mxCell id="66" parent="1" style="text;html=1;strokeColor=none;fillColor=#FBBF24;align=center;verticalAlign=middle;fontSize=12;fontColor=#1E293B;rounded=1;fontStyle=1;" value="默认(包)" vertex="1">
<mxGeometry height="30" width="80" x="270" y="950" as="geometry" />
</mxCell>
<mxCell id="67" parent="1" style="text;html=1;strokeColor=none;fillColor=#F87171;align=center;verticalAlign=middle;fontSize=12;fontColor=#1E293B;rounded=1;fontStyle=1;" value="private" vertex="1">
<mxGeometry height="30" width="80" x="365" y="950" as="geometry" />
</mxCell>
<mxCell id="68" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#94A3B8;" value="💡 private 构造方法常用于单例模式" vertex="1">
<mxGeometry height="30" width="340" x="480" y="950" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
5.3 易混淆点:构造方法 vs 同名普通方法
public class Demo {
// ✅ 这是构造方法(无返回类型)
public Demo() {
System.out.println("构造方法执行");
}
// ⚠️ 这是普通方法!(有返回类型 void)
public void Demo() {
System.out.println("普通方法执行");
}
}
public class Main {
public static void main(String[] args) {
Demo d = new Demo(); // 输出:构造方法执行
d.Demo(); // 输出:普通方法执行
}
}
⚠️ 注意:虽然语法允许定义与类名相同的普通方法,但这是极差的编码习惯!
六、构造方法与继承
6.1 子类构造调用父类构造
<mxfile>
<diagram id="super-constructor-rule" name="子类构造方法核心规则">
<mxGraphModel dx="820" dy="520" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="820" pageHeight="520">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="2" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#64748B;strokeWidth=2;" vertex="1" parent="1">
<mxGeometry x="20" y="20" width="780" height="480" as="geometry" />
</mxCell>
<mxCell id="3" value="🔑 子类构造方法核心规则" style="text;html=1;strokeColor=none;fillColor=#DC2626;align=center;verticalAlign=middle;fontSize=22;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
<mxGeometry x="20" y="20" width="780" height="50" as="geometry" />
</mxCell>
<mxCell id="4" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF2F2;strokeColor=#EF4444;strokeWidth=4;shadow=1;" vertex="1" parent="1">
<mxGeometry x="40" y="85" width="740" height="80" as="geometry" />
</mxCell>
<mxCell id="5" value="⚠️ 子类构造方法的【第一行】必须调用父类构造方法" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#DC2626;" vertex="1" parent="1">
<mxGeometry x="40" y="100" width="740" height="50" as="geometry" />
</mxCell>
<mxCell id="6" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#D1FAE5;strokeColor=#10B981;strokeWidth=3;shadow=1;" vertex="1" parent="1">
<mxGeometry x="40" y="180" width="355" height="185" as="geometry" />
</mxCell>
<mxCell id="7" value="✅ 显式调用" style="text;html=1;strokeColor=none;fillColor=#10B981;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
<mxGeometry x="40" y="180" width="355" height="38" as="geometry" />
</mxCell>
<mxCell id="8" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#475569;strokeWidth=2;" vertex="1" parent="1">
<mxGeometry x="55" y="230" width="325" height="80" as="geometry" />
</mxCell>
<mxCell id="9" value="public Child(String name) {" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#E2E8F0;fontFamily=Consolas;" vertex="1" parent="1">
<mxGeometry x="70" y="240" width="280" height="20" as="geometry" />
</mxCell>
<mxCell id="10" value="super(name);" style="text;html=1;strokeColor=none;fillColor=#065F46;align=left;verticalAlign=middle;fontSize=12;fontColor=#4ADE80;fontFamily=Consolas;fontStyle=1;rounded=1;" vertex="1" parent="1">
<mxGeometry x="85" y="262" width="120" height="22" as="geometry" />
</mxCell>
<mxCell id="11" value="// 第一行" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#6B7280;fontFamily=Consolas;" vertex="1" parent="1">
<mxGeometry x="215" y="262" width="100" height="22" as="geometry" />
</mxCell>
<mxCell id="12" value="}" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#E2E8F0;fontFamily=Consolas;" vertex="1" parent="1">
<mxGeometry x="70" y="286" width="50" height="20" as="geometry" />
</mxCell>
<mxCell id="13" value="💡 程序员主动写 super(参数)" style="text;html=1;strokeColor=none;fillColor=#BBF7D0;align=center;verticalAlign=middle;fontSize=12;fontColor=#166534;rounded=1;fontStyle=1;" vertex="1" parent="1">
<mxGeometry x="55" y="320" width="325" height="30" as="geometry" />
</mxCell>
<mxCell id="14" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#3B82F6;strokeWidth=3;shadow=1;" vertex="1" parent="1">
<mxGeometry x="425" y="180" width="355" height="185" as="geometry" />
</mxCell>
<mxCell id="15" value="🔄 隐式调用" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
<mxGeometry x="425" y="180" width="355" height="38" as="geometry" />
</mxCell>
<mxCell id="16" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#475569;strokeWidth=2;" vertex="1" parent="1">
<mxGeometry x="440" y="230" width="325" height="80" as="geometry" />
</mxCell>
<mxCell id="17" value="public Child() {" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#E2E8F0;fontFamily=Consolas;" vertex="1" parent="1">
<mxGeometry x="455" y="240" width="200" height="20" as="geometry" />
</mxCell>
<mxCell id="18" value="// super();" style="text;html=1;strokeColor=none;fillColor=#1E3A5F;align=left;verticalAlign=middle;fontSize=12;fontColor=#60A5FA;fontFamily=Consolas;fontStyle=2;rounded=1;" vertex="1" parent="1">
<mxGeometry x="470" y="262" width="100" height="22" as="geometry" />
</mxCell>
<mxCell id="19" value="← 编译器自动添加" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#6B7280;fontFamily=Consolas;" vertex="1" parent="1">
<mxGeometry x="580" y="262" width="150" height="22" as="geometry" />
</mxCell>
<mxCell id="20" value="}" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#E2E8F0;fontFamily=Consolas;" vertex="1" parent="1">
<mxGeometry x="455" y="286" width="50" height="20" as="geometry" />
</mxCell>
<mxCell id="21" value="💡 不写时,编译器自动加 super()" style="text;html=1;strokeColor=none;fillColor=#BFDBFE;align=center;verticalAlign=middle;fontSize=12;fontColor=#1E40AF;rounded=1;fontStyle=1;" vertex="1" parent="1">
<mxGeometry x="440" y="320" width="325" height="30" as="geometry" />
</mxCell>
<mxCell id="22" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=3;shadow=1;" vertex="1" parent="1">
<mxGeometry x="40" y="380" width="740" height="105" as="geometry" />
</mxCell>
<mxCell id="23" value="⚡ 为什么必须这样?" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
<mxGeometry x="40" y="380" width="180" height="32" as="geometry" />
</mxCell>
<mxCell id="24" value="父类" style="ellipse;whiteSpace=wrap;html=1;fillColor=#3B82F6;strokeColor=#1E40AF;strokeWidth=2;fontColor=#FFFFFF;fontSize=13;fontStyle=1;" vertex="1" parent="1">
<mxGeometry x="80" y="425" width="70" height="45" as="geometry" />
</mxCell>
<mxCell id="25" value="" style="endArrow=classic;html=1;strokeWidth=3;strokeColor=#F59E0B;" edge="1" parent="1">
<mxGeometry relative="1" as="geometry">
<mxPoint x="160" y="447" as="sourcePoint" />
<mxPoint x="220" y="447" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="26" value="子类" style="ellipse;whiteSpace=wrap;html=1;fillColor=#10B981;strokeColor=#047857;strokeWidth=2;fontColor=#FFFFFF;fontSize=13;fontStyle=1;" vertex="1" parent="1">
<mxGeometry x="230" y="425" width="70" height="45" as="geometry" />
</mxCell>
<mxCell id="27" value="继承" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=10;fontColor=#92400E;" vertex="1" parent="1">
<mxGeometry x="165" y="425" width="50" height="18" as="geometry" />
</mxCell>
<mxCell id="28" value="子类对象包含父类的属性和行为,必须先初始化父类部分" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#92400E;fontStyle=0;" vertex="1" parent="1">
<mxGeometry x="320" y="420" width="440" height="25" as="geometry" />
</mxCell>
<mxCell id="29" value="确保父类成员在子类使用前已正确初始化 ✓" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#92400E;fontStyle=0;" vertex="1" parent="1">
<mxGeometry x="320" y="450" width="440" height="25" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
6.2 代码示例
class Animal {
protected String name;
public Animal() {
System.out.println("Animal 无参构造");
}
public Animal(String name) {
this.name = name;
System.out.println("Animal 有参构造:" + name);
}
}
class Dog extends Animal {
private String breed;
// 隐式调用 super()
public Dog() {
// 编译器自动添加:super();
System.out.println("Dog 无参构造");
}
// 显式调用 super(name)
public Dog(String name, String breed) {
super(name); // ✅ 必须在第一行
this.breed = breed;
System.out.println("Dog 有参构造:" + breed);
}
}
// 测试
Dog d1 = new Dog();
// 输出:
// Animal 无参构造
// Dog 无参构造
Dog d2 = new Dog("旺财", "金毛");
// 输出:
// Animal 有参构造:旺财
// Dog 有参构造:金毛
6.3 构造方法调用顺序图
<mxfile>
<diagram id="constructor-execution-order" name="构造方法执行顺序">
<mxGraphModel dx="1281" dy="581" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="900" math="0" shadow="0">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="2" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#64748B;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="860" width="860" x="20" y="20" as="geometry" />
</mxCell>
<mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#7C3AED;align=center;verticalAlign=middle;fontSize=22;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="🔄 构造方法执行顺序" vertex="1">
<mxGeometry height="50" width="860" x="20" y="20" as="geometry" />
</mxCell>
<mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#475569;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="40" width="380" x="40" y="85" as="geometry" />
</mxCell>
<mxCell id="5" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=16;fontColor=#FBBF24;fontFamily=Consolas;fontStyle=1;" value="new Dog("旺财", "金毛")" vertex="1">
<mxGeometry height="40" width="380" x="40" y="85" as="geometry" />
</mxCell>
<mxCell id="6" parent="1" style="text;html=1;strokeColor=none;fillColor=#7C3AED;align=center;verticalAlign=middle;fontSize=14;fontColor=#FFFFFF;rounded=1;fontStyle=1;" value="执行过程 ▶" vertex="1">
<mxGeometry height="30" width="100" x="440" y="90" as="geometry" />
</mxCell>
<mxCell id="7" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EDE9FE;strokeColor=#7C3AED;strokeWidth=3;shadow=1;" value="" vertex="1">
<mxGeometry height="720" width="820" x="40" y="140" as="geometry" />
</mxCell>
<mxCell id="8" parent="1" style="text;html=1;strokeColor=none;fillColor=#7C3AED;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="📚 调用栈 (Call Stack)" vertex="1">
<mxGeometry height="38" width="820" x="40" y="140" as="geometry" />
</mxCell>
<mxCell id="9" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#3B82F6;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="55" width="280" x="60" y="195" as="geometry" />
</mxCell>
<mxCell id="10" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#3B82F6;strokeColor=#1E40AF;strokeWidth=2;fontColor=#FFFFFF;fontSize=14;fontStyle=1;" value="1" vertex="1">
<mxGeometry height="35" width="35" x="70" y="205" as="geometry" />
</mxCell>
<mxCell id="11" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#1E40AF;fontStyle=1;" value="Dog(name, breed) 开始执行" vertex="1">
<mxGeometry height="35" width="215" x="115" y="205" as="geometry" />
</mxCell>
<mxCell id="12" edge="1" parent="1" source="9" style="endArrow=classic;html=1;strokeWidth=2;strokeColor=#7C3AED;entryX=0.5;entryY=0;entryDx=0;entryDy=0;exitX=0.5;exitY=1;exitDx=0;exitDy=0;" target="14" value="">
<mxGeometry relative="1" as="geometry">
<mxPoint x="200" y="260" as="sourcePoint" />
<mxPoint x="200" y="290" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=#F3E8FF;align=center;verticalAlign=middle;fontSize=11;fontColor=#7C3AED;rounded=1;fontStyle=1;" value="super(name)" vertex="1">
<mxGeometry height="22" width="90" x="210" y="255" as="geometry" />
</mxCell>
<mxCell id="14" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#D1FAE5;strokeColor=#10B981;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="55" width="280" x="80" y="285" as="geometry" />
</mxCell>
<mxCell id="15" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#10B981;strokeColor=#047857;strokeWidth=2;fontColor=#FFFFFF;fontSize=14;fontStyle=1;" value="2" vertex="1">
<mxGeometry height="35" width="35" x="90" y="295" as="geometry" />
</mxCell>
<mxCell id="16" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#047857;fontStyle=1;" value="Animal(name) 开始执行" vertex="1">
<mxGeometry height="35" width="215" x="135" y="295" as="geometry" />
</mxCell>
<mxCell id="17" edge="1" parent="1" source="14" style="endArrow=classic;html=1;strokeWidth=2;strokeColor=#7C3AED;entryX=0.5;entryY=0;entryDx=0;entryDy=0;exitX=0.5;exitY=1;exitDx=0;exitDy=0;" target="19" value="">
<mxGeometry relative="1" as="geometry">
<mxPoint x="220" y="350" as="sourcePoint" />
<mxPoint x="220" y="380" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="18" parent="1" style="text;html=1;strokeColor=none;fillColor=#F3E8FF;align=center;verticalAlign=middle;fontSize=11;fontColor=#7C3AED;rounded=1;fontStyle=1;" value="隐式 super()" vertex="1">
<mxGeometry height="22" width="90" x="230" y="345" as="geometry" />
</mxCell>
<mxCell id="19" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="55" width="280" x="100" y="375" as="geometry" />
</mxCell>
<mxCell id="20" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#F59E0B;strokeColor=#D97706;strokeWidth=2;fontColor=#FFFFFF;fontSize=14;fontStyle=1;" value="3" vertex="1">
<mxGeometry height="35" width="35" x="110" y="385" as="geometry" />
</mxCell>
<mxCell id="21" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#D97706;fontStyle=1;" value="Object() 开始执行" vertex="1">
<mxGeometry height="35" width="215" x="155" y="385" as="geometry" />
</mxCell>
<mxCell id="22" edge="1" parent="1" source="19" style="endArrow=classic;html=1;strokeWidth=2;strokeColor=#10B981;exitX=1;exitY=0.5;exitDx=0;exitDy=0;entryX=0;entryY=0.5;entryDx=0;entryDy=0;curved=1;" target="24" value="">
<mxGeometry relative="1" as="geometry">
<mxPoint x="390" y="402" as="sourcePoint" />
<mxPoint x="450" y="402" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="23" parent="1" style="text;html=1;strokeColor=none;fillColor=#D1FAE5;align=center;verticalAlign=middle;fontSize=10;fontColor=#047857;rounded=1;" value="执行完毕,返回" vertex="1">
<mxGeometry height="20" width="90" x="390" y="380" as="geometry" />
</mxCell>
<mxCell id="24" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#D1FAE5;strokeColor=#10B981;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="100" width="350" x="500" y="375" as="geometry" />
</mxCell>
<mxCell id="25" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#10B981;strokeColor=#047857;strokeWidth=2;fontColor=#FFFFFF;fontSize=14;fontStyle=1;" value="4" vertex="1">
<mxGeometry height="35" width="35" x="510" y="385" as="geometry" />
</mxCell>
<mxCell id="26" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#047857;fontStyle=1;" value="Animal(name) 继续执行" vertex="1">
<mxGeometry height="30" width="200" x="555" y="385" as="geometry" />
</mxCell>
<mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=#1E293B;align=left;verticalAlign=middle;fontSize=11;fontColor=#4ADE80;fontFamily=Consolas;rounded=1;" value="→ this.name = name" vertex="1">
<mxGeometry height="22" width="160" x="520" y="420" as="geometry" />
</mxCell>
<mxCell id="28" parent="1" style="text;html=1;strokeColor=none;fillColor=#1E293B;align=left;verticalAlign=middle;fontSize=11;fontColor=#4ADE80;fontFamily=Consolas;rounded=1;" value="→ 打印 "Animal 有参构造:旺财"" vertex="1">
<mxGeometry height="22" width="310" x="520" y="447" as="geometry" />
</mxCell>
<mxCell id="29" edge="1" parent="1" source="24" style="endArrow=classic;html=1;strokeWidth=2;strokeColor=#10B981;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;curved=1;" target="31" value="">
<mxGeometry relative="1" as="geometry">
<mxPoint x="675" y="485" as="sourcePoint" />
<mxPoint x="675" y="520" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="30" parent="1" style="text;html=1;strokeColor=none;fillColor=#D1FAE5;align=center;verticalAlign=middle;fontSize=10;fontColor=#047857;rounded=1;" value="执行完毕,返回" vertex="1">
<mxGeometry height="20" width="90" x="580" y="480" as="geometry" />
</mxCell>
<mxCell id="31" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#3B82F6;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="100" width="350" x="500" y="510" as="geometry" />
</mxCell>
<mxCell id="32" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#3B82F6;strokeColor=#1E40AF;strokeWidth=2;fontColor=#FFFFFF;fontSize=14;fontStyle=1;" value="5" vertex="1">
<mxGeometry height="35" width="35" x="510" y="520" as="geometry" />
</mxCell>
<mxCell id="33" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#1E40AF;fontStyle=1;" value="Dog(name, breed) 继续执行" vertex="1">
<mxGeometry height="30" width="220" x="555" y="520" as="geometry" />
</mxCell>
<mxCell id="34" parent="1" style="text;html=1;strokeColor=none;fillColor=#1E293B;align=left;verticalAlign=middle;fontSize=11;fontColor=#60A5FA;fontFamily=Consolas;rounded=1;" value="→ this.breed = breed" vertex="1">
<mxGeometry height="22" width="160" x="520" y="555" as="geometry" />
</mxCell>
<mxCell id="35" parent="1" style="text;html=1;strokeColor=none;fillColor=#1E293B;align=left;verticalAlign=middle;fontSize=11;fontColor=#60A5FA;fontFamily=Consolas;rounded=1;" value="→ 打印 "Dog 有参构造:金毛"" vertex="1">
<mxGeometry height="22" width="280" x="520" y="582" as="geometry" />
</mxCell>
<mxCell id="36" edge="1" parent="1" source="31" style="endArrow=classic;html=1;strokeWidth=2;strokeColor=#10B981;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" target="38" value="">
<mxGeometry relative="1" as="geometry">
<mxPoint x="675" y="620" as="sourcePoint" />
<mxPoint x="675" y="660" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="37" parent="1" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=center;verticalAlign=middle;fontSize=10;fontColor=#1E40AF;rounded=1;" value="执行完毕" vertex="1">
<mxGeometry height="20" width="60" x="630" y="615" as="geometry" />
</mxCell>
<mxCell id="38" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#D1FAE5;strokeColor=#10B981;strokeWidth=3;shadow=1;" value="" vertex="1">
<mxGeometry height="50" width="250" x="550" y="645" as="geometry" />
</mxCell>
<mxCell id="39" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=16;fontColor=#047857;fontStyle=1;" value="✅ 对象创建成功!" vertex="1">
<mxGeometry height="50" width="250" x="550" y="645" as="geometry" />
</mxCell>
<mxCell id="40" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#475569;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="240" width="320" x="60" y="460" as="geometry" />
</mxCell>
<mxCell id="41" parent="1" style="text;html=1;strokeColor=none;fillColor=#475569;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="📊 类继承层次" vertex="1">
<mxGeometry height="28" width="120" x="60" y="460" as="geometry" />
</mxCell>
<mxCell id="42" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F59E0B;strokeColor=#D97706;strokeWidth=2;fontColor=#FFFFFF;fontSize=14;fontStyle=1;" value="Object" vertex="1">
<mxGeometry height="40" width="110" x="165" y="500" as="geometry" />
</mxCell>
<mxCell id="43" edge="1" parent="1" source="44" style="endArrow=block;html=1;strokeWidth=2;strokeColor=#94A3B8;endFill=0;exitX=0.5;exitY=0;exitDx=0;exitDy=0;entryX=0.5;entryY=1;entryDx=0;entryDy=0;" target="42" value="">
<mxGeometry relative="1" as="geometry">
<mxPoint x="220" y="560" as="sourcePoint" />
<mxPoint x="220" y="540" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="44" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#10B981;strokeColor=#047857;strokeWidth=2;fontColor=#FFFFFF;fontSize=14;fontStyle=1;" value="Animal" vertex="1">
<mxGeometry height="40" width="110" x="165" y="570" as="geometry" />
</mxCell>
<mxCell id="45" edge="1" parent="1" source="46" style="endArrow=block;html=1;strokeWidth=2;strokeColor=#94A3B8;endFill=0;exitX=0.5;exitY=0;exitDx=0;exitDy=0;entryX=0.5;entryY=1;entryDx=0;entryDy=0;" target="44" value="">
<mxGeometry relative="1" as="geometry">
<mxPoint x="220" y="630" as="sourcePoint" />
<mxPoint x="220" y="610" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="46" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#3B82F6;strokeColor=#1E40AF;strokeWidth=2;fontColor=#FFFFFF;fontSize=14;fontStyle=1;" value="Dog" vertex="1">
<mxGeometry height="40" width="110" x="165" y="640" as="geometry" />
</mxCell>
<mxCell id="47" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=10;fontColor=#94A3B8;" value="extends" vertex="1">
<mxGeometry height="20" width="50" x="280" y="545" as="geometry" />
</mxCell>
<mxCell id="48" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=10;fontColor=#94A3B8;" value="extends" vertex="1">
<mxGeometry height="20" width="50" x="280" y="615" as="geometry" />
</mxCell>
<mxCell id="49" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=3;shadow=1;" value="" vertex="1">
<mxGeometry height="55" width="750" x="75" y="770" as="geometry" />
</mxCell>
<mxCell id="50" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="💡 执行顺序总结" vertex="1">
<mxGeometry height="30" width="115" x="75" y="770" as="geometry" />
</mxCell>
<mxCell id="51" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F59E0B;strokeColor=#D97706;strokeWidth=2;fontColor=#FFFFFF;fontSize=13;fontStyle=1;" value="Object()" vertex="1">
<mxGeometry height="35" width="90" x="210" y="780" as="geometry" />
</mxCell>
<mxCell id="52" edge="1" parent="1" style="endArrow=classic;html=1;strokeWidth=3;strokeColor=#7C3AED;" value="">
<mxGeometry relative="1" as="geometry">
<mxPoint x="300" y="797.08" as="sourcePoint" />
<mxPoint x="360" y="797.08" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="53" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#10B981;strokeColor=#047857;strokeWidth=2;fontColor=#FFFFFF;fontSize=13;fontStyle=1;" value="Animal()" vertex="1">
<mxGeometry height="35" width="90" x="360" y="780" as="geometry" />
</mxCell>
<mxCell id="54" edge="1" parent="1" style="endArrow=classic;html=1;strokeWidth=3;strokeColor=#7C3AED;" value="">
<mxGeometry relative="1" as="geometry">
<mxPoint x="450" y="797.08" as="sourcePoint" />
<mxPoint x="510" y="797.08" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="55" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#3B82F6;strokeColor=#1E40AF;strokeWidth=2;fontColor=#FFFFFF;fontSize=13;fontStyle=1;" value="Dog()" vertex="1">
<mxGeometry height="35" width="90" x="510" y="780" as="geometry" />
</mxCell>
<mxCell id="56" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontColor=#92400E;fontStyle=1;" value="从顶层父类到子类" vertex="1">
<mxGeometry height="35" width="180" x="660" y="780" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
6.4 常见错误:父类没有无参构造
class Parent {
public Parent(String name) { // 只有有参构造
System.out.println("Parent: " + name);
}
}
class Child extends Parent {
public Child() {
// 编译器隐式添加 super(),但 Parent 没有无参构造!
// ❌ 编译错误!
}
}
解决方案:
// 方案 1:子类显式调用父类有参构造
class Child extends Parent {
public Child() {
super("默认名称"); // ✅ 显式调用
}
}
// 方案 2:父类添加无参构造
class Parent {
public Parent() { } // ✅ 添加无参构造
public Parent(String name) {
System.out.println("Parent: " + name);
}
}
七、最佳实践
7.1 实践规则一览表
| 序号 | 规则 | 原因 |
|---|---|---|
| 1 | 始终保留无参构造 | 框架反射、序列化需要 |
| 2 | 使用 this() 避免重复 | 代码复用,统一初始化 |
| 3 | 参数校验放构造中 | 确保对象状态合法 |
| 4 | 避免调用可重写方法 | 防止子类未初始化完成 |
| 5 | 考虑使用建造者模式 | 参数过多时更清晰 |
7.2 实践 1:始终保留无参构造
public class User {
private String username;
private String email;
// ✅ 始终保留无参构造
public User() {
}
public User(String username, String email) {
this.username = username;
this.email = email;
}
}
需要无参构造的场景:
<mxfile>
<diagram id="no-arg-constructor-scenarios" name="需要无参构造的场景">
<mxGraphModel dx="1281" dy="581" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="750" math="0" shadow="0">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="2" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#64748B;strokeWidth=2;shadow=1;" value="" vertex="1">
<mxGeometry height="710" width="860" x="20" y="20" as="geometry" />
</mxCell>
<mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=22;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="📋 需要无参构造的场景" vertex="1">
<mxGeometry height="55" width="860" x="20" y="20" as="geometry" />
</mxCell>
<mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EEF2FF;strokeColor=#6366F1;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="190" width="820" x="40" y="95" as="geometry" />
</mxCell>
<mxCell id="5" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#6366F1;strokeColor=#4338CA;strokeWidth=2;fontColor=#FFFFFF;fontSize=18;fontStyle=1;" value="1" vertex="1">
<mxGeometry height="40" width="40" x="55" y="110" as="geometry" />
</mxCell>
<mxCell id="6" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=18;fontColor=#4338CA;fontStyle=1;" value="框架反射创建对象" vertex="1">
<mxGeometry height="40" width="200" x="105" y="110" as="geometry" />
</mxCell>
<mxCell id="7" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#475569;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="110" width="780" x="60" y="160" as="geometry" />
</mxCell>
<mxCell id="8" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#6B7280;fontFamily=Consolas;" value="// Spring/MyBatis 等框架" vertex="1">
<mxGeometry height="28" width="300" x="80" y="170" as="geometry" />
</mxCell>
<mxCell id="9" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#A5B4FC;fontFamily=Consolas;fontStyle=1;" value="Class.forName("User").newInstance();" vertex="1">
<mxGeometry height="30" width="400" x="80" y="198" as="geometry" />
</mxCell>
<mxCell id="10" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#6B7280;fontFamily=Consolas;" value="// 调用无参构造" vertex="1">
<mxGeometry height="28" width="200" x="80" y="228" as="geometry" />
</mxCell>
<mxCell id="11" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=#16A34A;strokeWidth=2;fontColor=#FFFFFF;fontSize=12;fontStyle=1;" value="Spring" vertex="1">
<mxGeometry height="30" width="70" x="580" y="175" as="geometry" />
</mxCell>
<mxCell id="12" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EF4444;strokeColor=#DC2626;strokeWidth=2;fontColor=#FFFFFF;fontSize=12;fontStyle=1;" value="MyBatis" vertex="1">
<mxGeometry height="30" width="70" x="660" y="175" as="geometry" />
</mxCell>
<mxCell id="13" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#3B82F6;strokeColor=#2563EB;strokeWidth=2;fontColor=#FFFFFF;fontSize=12;fontStyle=1;" value="Struts" vertex="1">
<mxGeometry height="30" width="70" x="740" y="175" as="geometry" />
</mxCell>
<mxCell id="14" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="175" width="820" x="40" y="300" as="geometry" />
</mxCell>
<mxCell id="15" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#F59E0B;strokeColor=#D97706;strokeWidth=2;fontColor=#FFFFFF;fontSize=18;fontStyle=1;" value="2" vertex="1">
<mxGeometry height="40" width="40" x="55" y="315" as="geometry" />
</mxCell>
<mxCell id="16" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=18;fontColor=#D97706;fontStyle=1;" value="序列化/反序列化" vertex="1">
<mxGeometry height="40" width="200" x="105" y="315" as="geometry" />
</mxCell>
<mxCell id="17" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#475569;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="95" width="780" x="60" y="365" as="geometry" />
</mxCell>
<mxCell id="18" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#6B7280;fontFamily=Consolas;" value="// Jackson、Gson 等 JSON 库" vertex="1">
<mxGeometry height="28" width="320" x="80" y="375" as="geometry" />
</mxCell>
<mxCell id="19" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#FCD34D;fontFamily=Consolas;fontStyle=1;" value="objectMapper.readValue(json, User.class);" vertex="1">
<mxGeometry height="30" width="420" x="80" y="405" as="geometry" />
</mxCell>
<mxCell id="20" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#0EA5E9;strokeColor=#0284C7;strokeWidth=2;fontColor=#FFFFFF;fontSize=12;fontStyle=1;" value="Jackson" vertex="1">
<mxGeometry height="30" width="70" x="580" y="380" as="geometry" />
</mxCell>
<mxCell id="21" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#8B5CF6;strokeColor=#7C3AED;strokeWidth=2;fontColor=#FFFFFF;fontSize=12;fontStyle=1;" value="Gson" vertex="1">
<mxGeometry height="30" width="70" x="660" y="380" as="geometry" />
</mxCell>
<mxCell id="22" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EC4899;strokeColor=#DB2777;strokeWidth=2;fontColor=#FFFFFF;fontSize=12;fontStyle=1;" value="Fastjson" vertex="1">
<mxGeometry height="30" width="70" x="740" y="380" as="geometry" />
</mxCell>
<mxCell id="23" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#22C55E;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="210" width="820" x="40" y="490" as="geometry" />
</mxCell>
<mxCell id="24" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=#16A34A;strokeWidth=2;fontColor=#FFFFFF;fontSize=18;fontStyle=1;" value="3" vertex="1">
<mxGeometry height="40" width="40" x="55" y="505" as="geometry" />
</mxCell>
<mxCell id="25" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=18;fontColor=#16A34A;fontStyle=1;" value="JPA/Hibernate 实体类" vertex="1">
<mxGeometry height="40" width="250" x="105" y="505" as="geometry" />
</mxCell>
<mxCell id="26" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#475569;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="130" width="780" x="60" y="555" as="geometry" />
</mxCell>
<mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=#374151;align=left;verticalAlign=middle;fontSize=14;fontColor=#FBBF24;fontFamily=Consolas;fontStyle=1;rounded=1;" value="@Entity" vertex="1">
<mxGeometry height="26" width="90" x="80" y="565" as="geometry" />
</mxCell>
<mxCell id="28" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#86EFAC;fontFamily=Consolas;fontStyle=1;" value="public class User {" vertex="1">
<mxGeometry height="26" width="220" x="80" y="595" as="geometry" />
</mxCell>
<mxCell id="29" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#86EFAC;fontFamily=Consolas;fontStyle=1;" value="public User() { }" vertex="1">
<mxGeometry height="26" width="200" x="110" y="623" as="geometry" />
</mxCell>
<mxCell id="30" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#6B7280;fontFamily=Consolas;" value="// 必须有无参构造" vertex="1">
<mxGeometry height="26" width="200" x="310" y="623" as="geometry" />
</mxCell>
<mxCell id="31" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#86EFAC;fontFamily=Consolas;fontStyle=1;" value="}" vertex="1">
<mxGeometry height="26" width="30" x="80" y="651" as="geometry" />
</mxCell>
<mxCell id="32" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F97316;strokeColor=#EA580C;strokeWidth=2;fontColor=#FFFFFF;fontSize=12;fontStyle=1;" value="JPA" vertex="1">
<mxGeometry height="30" width="70" x="580" y="570" as="geometry" />
</mxCell>
<mxCell id="33" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#14B8A6;strokeColor=#0D9488;strokeWidth=2;fontColor=#FFFFFF;fontSize=12;fontStyle=1;" value="Hibernate" vertex="1">
<mxGeometry height="30" width="80" x="660" y="570" as="geometry" />
</mxCell>
<mxCell id="34" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#6366F1;strokeColor=#4F46E5;strokeWidth=2;fontColor=#FFFFFF;fontSize=11;fontStyle=1;" value="EclipseLink" vertex="1">
<mxGeometry height="30" width="80" x="750" y="570" as="geometry" />
</mxCell>
<mxCell id="35" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEE2E2;align=center;verticalAlign=middle;fontSize=12;fontColor=#DC2626;fontStyle=1;rounded=1;" value="⚠️ 必需" vertex="1">
<mxGeometry height="26" width="70" x="580" y="610" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
7.3 实践 2:使用 this() 避免代码重复
// ❌ 不好的写法:重复代码
public class Product {
private String name;
private double price;
private String category;
public Product() {
this.name = "未命名";
this.price = 0.0;
this.category = "未分类";
validate(); // 重复
log(); // 重复
}
public Product(String name) {
this.name = name;
this.price = 0.0;
this.category = "未分类";
validate(); // 重复
log(); // 重复
}
public Product(String name, double price, String category) {
this.name = name;
this.price = price;
this.category = category;
validate(); // 重复
log(); // 重复
}
}
// ✅ 好的写法:使用 this() 委托
public class Product {
private String name;
private double price;
private String category;
// 全参构造(核心构造方法)
public Product(String name, double price, String category) {
this.name = name;
this.price = price;
this.category = category;
validate(); // 只写一次
log(); // 只写一次
}
// 委托给全参构造
public Product() {
this("未命名", 0.0, "未分类");
}
public Product(String name) {
this(name, 0.0, "未分类");
}
public Product(String name, double price) {
this(name, price, "未分类");
}
}
7.4 实践 3:在构造方法中进行参数校验
public class BankAccount {
private String accountNumber;
private double balance;
public BankAccount(String accountNumber, double initialBalance) {
// ✅ 参数校验
if (accountNumber == null || accountNumber.trim().isEmpty()) {
throw new IllegalArgumentException("账号不能为空");
}
if (initialBalance < 0) {
throw new IllegalArgumentException("初始余额不能为负");
}
this.accountNumber = accountNumber;
this.balance = initialBalance;
}
}
// 使用
BankAccount acc1 = new BankAccount("123456", 1000); // ✅ 正常
BankAccount acc2 = new BankAccount("", 100); // ❌ 抛出异常
BankAccount acc3 = new BankAccount("123", -100); // ❌ 抛出异常
7.5 实践 4:避免在构造方法中调用可重写方法
// ❌ 危险的写法
class Parent {
public Parent() {
init(); // 调用可重写方法
}
public void init() {
System.out.println("Parent init");
}
}
class Child extends Parent {
private String name;
public Child(String name) {
super(); // 调用父类构造
this.name = name;
}
@Override
public void init() {
// ⚠️ 此时 name 还是 null!因为子类构造还没执行完
System.out.println("Child init: " + name.toUpperCase()); // NullPointerException!
}
}
问题分析图:
<mxfile>
<diagram id="constructor-override-problem" name="构造方法调用可重写方法的问题">
<mxGraphModel dx="1281" dy="581" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="1000" pageHeight="850" math="0" shadow="0">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="2" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#475569;strokeWidth=2;shadow=1;arcSize=15;" value="" vertex="1">
<mxGeometry height="900" width="960" x="20" y="20" as="geometry" />
</mxCell>
<mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#DC2626;align=center;verticalAlign=middle;fontSize=22;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="⚠️ 构造方法调用可重写方法的问题" vertex="1">
<mxGeometry height="55" width="960" x="20" y="20" as="geometry" />
</mxCell>
<mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#0F172A;strokeColor=#334155;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="45" width="300" x="40" y="90" as="geometry" />
</mxCell>
<mxCell id="5" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=18;fontColor=#FBBF24;fontFamily=Consolas;fontStyle=1;" value="new Child("张三")" vertex="1">
<mxGeometry height="45" width="300" x="40" y="90" as="geometry" />
</mxCell>
<mxCell id="6" parent="1" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=14;fontColor=#FFFFFF;rounded=1;fontStyle=1;" value="执行顺序 ▼" vertex="1">
<mxGeometry height="32" width="100" x="360" y="97" as="geometry" />
</mxCell>
<mxCell id="7" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#3B82F6;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="55" width="440" x="40" y="150" as="geometry" />
</mxCell>
<mxCell id="8" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#3B82F6;strokeColor=#1E40AF;strokeWidth=2;fontColor=#FFFFFF;fontSize=16;fontStyle=1;" value="1" vertex="1">
<mxGeometry height="32" width="32" x="55" y="162" as="geometry" />
</mxCell>
<mxCell id="9" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=16;fontColor=#1E40AF;fontStyle=1;" value="Child 构造开始" vertex="1">
<mxGeometry height="32" width="370" x="95" y="162" as="geometry" />
</mxCell>
<mxCell id="10" edge="1" parent="1" source="7" style="endArrow=classic;html=1;strokeWidth=3;strokeColor=#6366F1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" target="11" value="">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="11" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#D1FAE5;strokeColor=#10B981;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="55" width="440" x="40" y="220" as="geometry" />
</mxCell>
<mxCell id="12" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#10B981;strokeColor=#047857;strokeWidth=2;fontColor=#FFFFFF;fontSize=16;fontStyle=1;" value="2" vertex="1">
<mxGeometry height="32" width="32" x="55" y="232" as="geometry" />
</mxCell>
<mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=16;fontColor=#047857;fontStyle=1;" value="调用 super() → Parent 构造开始" vertex="1">
<mxGeometry height="32" width="370" x="95" y="232" as="geometry" />
</mxCell>
<mxCell id="14" edge="1" parent="1" source="11" style="endArrow=classic;html=1;strokeWidth=3;strokeColor=#6366F1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" target="15" value="">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="15" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="55" width="440" x="40" y="290" as="geometry" />
</mxCell>
<mxCell id="16" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#F59E0B;strokeColor=#D97706;strokeWidth=2;fontColor=#FFFFFF;fontSize=16;fontStyle=1;" value="3" vertex="1">
<mxGeometry height="32" width="32" x="55" y="302" as="geometry" />
</mxCell>
<mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=16;fontColor=#D97706;fontStyle=1;" value="Parent 构造调用 init()" vertex="1">
<mxGeometry height="32" width="370" x="95" y="302" as="geometry" />
</mxCell>
<mxCell id="18" edge="1" parent="1" source="15" style="endArrow=classic;html=1;strokeWidth=3;strokeColor=#6366F1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" target="19" value="">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="19" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEE2E2;strokeColor=#EF4444;strokeWidth=3;" value="" vertex="1">
<mxGeometry height="55" width="440" x="40" y="360" as="geometry" />
</mxCell>
<mxCell id="20" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#EF4444;strokeColor=#DC2626;strokeWidth=2;fontColor=#FFFFFF;fontSize=16;fontStyle=1;" value="4" vertex="1">
<mxGeometry height="32" width="32" x="55" y="372" as="geometry" />
</mxCell>
<mxCell id="21" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=16;fontColor=#DC2626;fontStyle=1;" value="由于多态,实际调用 Child.init()" vertex="1">
<mxGeometry height="32" width="300" x="95" y="372" as="geometry" />
</mxCell>
<mxCell id="22" parent="1" style="text;html=1;strokeColor=none;fillColor=#DC2626;align=center;verticalAlign=middle;fontSize=12;fontColor=#FFFFFF;fontStyle=1;rounded=1;" value="⚠️ 问题!" vertex="1">
<mxGeometry height="24" width="70" x="390" y="377" as="geometry" />
</mxCell>
<mxCell id="23" edge="1" parent="1" source="19" style="endArrow=classic;html=1;strokeWidth=3;strokeColor=#6366F1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" target="24" value="">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="24" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEE2E2;strokeColor=#EF4444;strokeWidth=3;" value="" vertex="1">
<mxGeometry height="55" width="440" x="40" y="430" as="geometry" />
</mxCell>
<mxCell id="25" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#EF4444;strokeColor=#DC2626;strokeWidth=2;fontColor=#FFFFFF;fontSize=16;fontStyle=1;" value="5" vertex="1">
<mxGeometry height="32" width="32" x="55" y="442" as="geometry" />
</mxCell>
<mxCell id="26" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=16;fontColor=#DC2626;fontStyle=1;" value="Child.init() 使用 name" vertex="1">
<mxGeometry height="32" width="220" x="95" y="442" as="geometry" />
</mxCell>
<mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=#7F1D1D;align=center;verticalAlign=middle;fontSize=12;fontColor=#FECACA;fontStyle=1;rounded=1;" value="name 还是 null!" vertex="1">
<mxGeometry height="24" width="130" x="320" y="447" as="geometry" />
</mxCell>
<mxCell id="28" edge="1" parent="1" source="24" style="endArrow=classic;html=1;strokeWidth=3;strokeColor=#6366F1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" target="29" value="">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="29" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DC2626;strokeColor=#991B1B;strokeWidth=3;shadow=1;" value="" vertex="1">
<mxGeometry height="55" width="440" x="40" y="500" as="geometry" />
</mxCell>
<mxCell id="30" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#DC2626;strokeWidth=2;fontColor=#DC2626;fontSize=16;fontStyle=1;" value="6" vertex="1">
<mxGeometry height="32" width="32" x="55" y="512" as="geometry" />
</mxCell>
<mxCell id="31" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=18;fontColor=#FFFFFF;fontStyle=1;" value="💥 NullPointerException!" vertex="1">
<mxGeometry height="32" width="350" x="95" y="512" as="geometry" />
</mxCell>
<mxCell id="32" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#0F172A;strokeColor=#EF4444;strokeWidth=3;shadow=1;" value="" vertex="1">
<mxGeometry height="220" width="450" x="510" y="150" as="geometry" />
</mxCell>
<mxCell id="33" parent="1" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="🔍 Child 对象的状态" vertex="1">
<mxGeometry height="40" width="450" x="510" y="150" as="geometry" />
</mxCell>
<mxCell id="34" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#475569;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="150" width="410" x="530" y="205" as="geometry" />
</mxCell>
<mxCell id="35" parent="1" style="text;html=1;strokeColor=none;fillColor=#334155;align=center;verticalAlign=middle;fontSize=14;fontColor=#94A3B8;fontStyle=1;rounded=1;" value="Child 对象" vertex="1">
<mxGeometry height="28" width="100" x="545" y="215" as="geometry" />
</mxCell>
<mxCell id="36" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#7F1D1D;strokeColor=#DC2626;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="45" width="380" x="545" y="255" as="geometry" />
</mxCell>
<mxCell id="37" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=15;fontColor=#F87171;fontFamily=Consolas;fontStyle=1;" value="name =" vertex="1">
<mxGeometry height="35" width="70" x="560" y="260" as="geometry" />
</mxCell>
<mxCell id="38" parent="1" style="text;html=1;strokeColor=none;fillColor=#DC2626;align=center;verticalAlign=middle;fontSize=16;fontColor=#FFFFFF;fontFamily=Consolas;fontStyle=1;rounded=1;" value="null" vertex="1">
<mxGeometry height="28" width="60" x="640" y="263" as="geometry" />
</mxCell>
<mxCell id="39" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#FBBF24;fontStyle=1;" value="← 还没被赋值!" vertex="1">
<mxGeometry height="35" width="150" x="720" y="260" as="geometry" />
</mxCell>
<mxCell id="40" parent="1" style="text;html=1;strokeColor=none;fillColor=#374151;align=center;verticalAlign=middle;fontSize=13;fontColor=#F59E0B;fontStyle=1;rounded=1;" value="💡 子类属性赋值在父类构造之后" vertex="1">
<mxGeometry height="30" width="280" x="545" y="310" as="geometry" />
</mxCell>
<mxCell id="41" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#0F172A;strokeColor=#6366F1;strokeWidth=3;shadow=1;" value="" vertex="1">
<mxGeometry height="240" width="450" x="510" y="390" as="geometry" />
</mxCell>
<mxCell id="42" parent="1" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="📊 执行时间线" vertex="1">
<mxGeometry height="40" width="450" x="510" y="390" as="geometry" />
</mxCell>
<mxCell id="43" edge="1" parent="1" style="endArrow=none;html=1;strokeWidth=4;strokeColor=#475569;" value="">
<mxGeometry relative="1" as="geometry">
<mxPoint x="600" y="590" as="sourcePoint" />
<mxPoint x="600" y="450" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="44" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontColor=#64748B;fontStyle=2;" value="时间" vertex="1">
<mxGeometry height="20" width="50" x="580" y="600" as="geometry" />
</mxCell>
<mxCell id="45" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#10B981;strokeColor=#047857;strokeWidth=2;fontColor=#FFFFFF;fontSize=12;fontStyle=1;" value="Parent 构造" vertex="1">
<mxGeometry height="35" width="100" x="620" y="450" as="geometry" />
</mxCell>
<mxCell id="46" edge="1" parent="1" source="45" style="endArrow=classic;html=1;strokeWidth=2;strokeColor=#94A3B8;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" target="47" value="">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="47" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F59E0B;strokeColor=#D97706;strokeWidth=2;fontColor=#FFFFFF;fontSize=12;fontStyle=1;" value="调用 init()" vertex="1">
<mxGeometry height="35" width="100" x="620" y="495" as="geometry" />
</mxCell>
<mxCell id="48" edge="1" parent="1" source="47" style="endArrow=classic;html=1;strokeWidth=2;strokeColor=#EF4444;exitX=1;exitY=0.5;exitDx=0;exitDy=0;entryX=0;entryY=0.5;entryDx=0;entryDy=0;dashed=1;dashPattern=8 8;" target="49" value="">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="49" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EF4444;strokeColor=#DC2626;strokeWidth=2;fontColor=#FFFFFF;fontSize=12;fontStyle=1;" value="Child.init() 执行" vertex="1">
<mxGeometry height="35" width="120" x="760" y="495" as="geometry" />
</mxCell>
<mxCell id="50" parent="1" style="text;html=1;strokeColor=none;fillColor=#7F1D1D;align=center;verticalAlign=middle;fontSize=10;fontColor=#FECACA;rounded=1;" value="多态" vertex="1">
<mxGeometry height="18" width="40" x="725" y="475" as="geometry" />
</mxCell>
<mxCell id="51" edge="1" parent="1" source="49" style="endArrow=classic;html=1;strokeWidth=2;strokeColor=#94A3B8;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" target="52" value="">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="52" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#7F1D1D;strokeColor=#DC2626;strokeWidth=2;fontColor=#FECACA;fontSize=12;fontStyle=1;" value="访问 name" vertex="1">
<mxGeometry height="35" width="120" x="760" y="545" as="geometry" />
</mxCell>
<mxCell id="53" edge="1" parent="1" style="endArrow=classic;html=1;strokeWidth=2;strokeColor=#94A3B8;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" value="">
<mxGeometry relative="1" as="geometry">
<mxPoint x="865" y="775" as="sourcePoint" />
<mxPoint x="865" y="790" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="54" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DC2626;strokeColor=#991B1B;strokeWidth=2;fontColor=#FFFFFF;fontSize=14;fontStyle=1;" value="💥 NPE" vertex="1">
<mxGeometry height="35" width="90" x="700" y="589.5" as="geometry" />
</mxCell>
<mxCell id="55" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#334155;strokeColor=#475569;strokeWidth=2;fontColor=#64748B;fontSize=12;fontStyle=1;dashed=1;dashPattern=8 8;" value="name 赋值" vertex="1">
<mxGeometry height="35" width="100" x="620" y="545" as="geometry" />
</mxCell>
<mxCell id="56" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=10;fontColor=#64748B;fontStyle=2;" value="永远不会执行到" vertex="1">
<mxGeometry height="18" width="110" x="655" y="827" as="geometry" />
</mxCell>
<mxCell id="57" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=3;shadow=1;" value="" vertex="1">
<mxGeometry height="100" width="920" x="40" y="760" as="geometry" />
</mxCell>
<mxCell id="58" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="💡 最佳实践" vertex="1">
<mxGeometry height="35" width="120" x="85" y="770" as="geometry" />
</mxCell>
<mxCell id="59" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEE2E2;align=center;verticalAlign=middle;fontSize=14;fontColor=#DC2626;fontStyle=1;rounded=1;" value="❌ 不要在构造方法中调用可被重写的方法" vertex="1">
<mxGeometry height="35" width="350" x="105" y="815" as="geometry" />
</mxCell>
<mxCell id="60" parent="1" style="text;html=1;strokeColor=none;fillColor=#D1FAE5;align=center;verticalAlign=middle;fontSize=14;fontColor=#047857;fontStyle=1;rounded=1;" value="✅ 使用 final/private 方法或在构造完成后初始化" vertex="1">
<mxGeometry height="35" width="400" x="475" y="815" as="geometry" />
</mxCell>
<mxCell id="61" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#6366F1;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="110" width="920" x="40" y="640" as="geometry" />
</mxCell>
<mxCell id="62" parent="1" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="🔑 根本原因" vertex="1">
<mxGeometry height="30" width="110" x="55" y="650" as="geometry" />
</mxCell>
<mxCell id="63" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#10B981;strokeColor=#047857;strokeWidth=2;fontColor=#FFFFFF;fontSize=13;fontStyle=1;" value="父类构造执行时" vertex="1">
<mxGeometry height="40" width="130" x="55" y="695" as="geometry" />
</mxCell>
<mxCell id="64" edge="1" parent="1" style="endArrow=classic;html=1;strokeWidth=3;strokeColor=#94A3B8;" value="">
<mxGeometry relative="1" as="geometry">
<mxPoint x="195" y="715" as="sourcePoint" />
<mxPoint x="255" y="715" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="65" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EF4444;strokeColor=#DC2626;strokeWidth=2;fontColor=#FFFFFF;fontSize=13;fontStyle=1;" value="子类属性未初始化" vertex="1">
<mxGeometry height="40" width="150" x="265" y="695" as="geometry" />
</mxCell>
<mxCell id="66" edge="1" parent="1" style="endArrow=classic;html=1;strokeWidth=3;strokeColor=#94A3B8;" value="">
<mxGeometry relative="1" as="geometry">
<mxPoint x="425" y="715" as="sourcePoint" />
<mxPoint x="485" y="715" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="67" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F59E0B;strokeColor=#D97706;strokeWidth=2;fontColor=#FFFFFF;fontSize=13;fontStyle=1;" value="多态调用子类方法" vertex="1">
<mxGeometry height="40" width="150" x="495" y="695" as="geometry" />
</mxCell>
<mxCell id="68" edge="1" parent="1" style="endArrow=classic;html=1;strokeWidth=3;strokeColor=#94A3B8;" value="">
<mxGeometry relative="1" as="geometry">
<mxPoint x="655" y="715" as="sourcePoint" />
<mxPoint x="715" y="715" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="69" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DC2626;strokeColor=#991B1B;strokeWidth=2;fontColor=#FFFFFF;fontSize=13;fontStyle=1;" value="访问 null 值 💥" vertex="1">
<mxGeometry height="40" width="130" x="725" y="695" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
解决方案:
// ✅ 使用 final 方法或 private 方法
class Parent {
public Parent() {
initInternal(); // 调用 private 方法
}
private void initInternal() { // 不能被重写
System.out.println("Parent init");
}
}
7.6 实践 5:参数过多时使用建造者模式
// ❌ 构造参数过多,难以阅读
User user = new User("张三", 20, "男", "北京", "100000",
"13800138000", "zhangsan@email.com", true);
// ✅ 使用建造者模式
User user = User.builder()
.name("张三")
.age(20)
.gender("男")
.city("北京")
.build();
建造者模式实现:
public class User {
private final String name;
private final int age;
private final String gender;
private final String city;
// 私有构造,只能通过 Builder 创建
private User(Builder builder) {
this.name = builder.name;
this.age = builder.age;
this.gender = builder.gender;
this.city = builder.city;
}
// 静态方法获取 Builder
public static Builder builder() {
return new Builder();
}
// 静态内部类 Builder
public static class Builder {
private String name;
private int age;
private String gender;
private String city;
public Builder name(String name) {
this.name = name;
return this;
}
public Builder age(int age) {
this.age = age;
return this;
}
public Builder gender(String gender) {
this.gender = gender;
return this;
}
public Builder city(String city) {
this.city = city;
return this;
}
public User build() {
return new User(this);
}
}
}
八、核心总结
8.1 知识要点速查表
| 主题 | 要点 |
|---|---|
| 默认构造 | 没有任何构造方法时编译器自动生成 |
| 消失条件 | 定义任何构造方法后默认构造消失 |
| 重载 | 同类中多个构造,参数不同 |
| this() | 调用本类其他构造,必须在第一行 |
| super() | 调用父类构造,必须在第一行 |
| 执行顺序 | Object → 父类 → 子类 |
8.2 构造方法完整生命周期图
<mxfile>
<diagram id="new-student-flow" name="new Student完整流程">
<mxGraphModel dx="704" dy="734" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="700" pageHeight="900" math="0" shadow="0">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="2" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fafafa;strokeColor=#424242;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="800" width="620" x="40" y="30" as="geometry" />
</mxCell>
<mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#1565c0;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=18;fontStyle=1;fontColor=#ffffff;" value="new Student("张三", 20) 完整流程" vertex="1">
<mxGeometry height="50" width="620" x="40" y="30" as="geometry" />
</mxCell>
<mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#1976d2;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="70" width="540" x="80" y="100" as="geometry" />
</mxCell>
<mxCell id="5" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#1976d2;strokeColor=none;fontColor=#ffffff;fontSize=16;fontStyle=1;" value="1" vertex="1">
<mxGeometry height="40" width="40" x="95" y="115" as="geometry" />
</mxCell>
<mxCell id="6" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;fontStyle=1;fontColor=#1565c0;" value="类加载检查" vertex="1">
<mxGeometry height="25" width="120" x="145" y="110" as="geometry" />
</mxCell>
<mxCell id="7" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#424242;" value="Student.class 是否已加载?否则先加载" vertex="1">
<mxGeometry height="25" width="450" x="145" y="135" as="geometry" />
</mxCell>
<mxCell id="8" edge="1" parent="1" source="4" style="endArrow=classic;html=1;rounded=0;strokeWidth=2;strokeColor=#1976d2;entryX=0.5;entryY=0;entryDx=0;entryDy=0;exitX=0.5;exitY=1;exitDx=0;exitDy=0;" target="9" value="">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="9" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e8f5e9;strokeColor=#43a047;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="70" width="540" x="80" y="195" as="geometry" />
</mxCell>
<mxCell id="10" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#43a047;strokeColor=none;fontColor=#ffffff;fontSize=16;fontStyle=1;" value="2" vertex="1">
<mxGeometry height="40" width="40" x="95" y="210" as="geometry" />
</mxCell>
<mxCell id="11" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;fontStyle=1;fontColor=#2e7d32;" value="分配内存" vertex="1">
<mxGeometry height="25" width="120" x="145" y="205" as="geometry" />
</mxCell>
<mxCell id="12" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#424242;" value="在堆中为 Student 对象分配空间" vertex="1">
<mxGeometry height="25" width="450" x="145" y="230" as="geometry" />
</mxCell>
<mxCell id="13" edge="1" parent="1" source="9" style="endArrow=classic;html=1;rounded=0;strokeWidth=2;strokeColor=#43a047;entryX=0.5;entryY=0;entryDx=0;entryDy=0;exitX=0.5;exitY=1;exitDx=0;exitDy=0;" target="14" value="">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="14" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff3e0;strokeColor=#fb8c00;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="70" width="540" x="80" y="290" as="geometry" />
</mxCell>
<mxCell id="15" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#fb8c00;strokeColor=none;fontColor=#ffffff;fontSize=16;fontStyle=1;" value="3" vertex="1">
<mxGeometry height="40" width="40" x="95" y="305" as="geometry" />
</mxCell>
<mxCell id="16" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;fontStyle=1;fontColor=#ef6c00;" value="默认初始化" vertex="1">
<mxGeometry height="25" width="120" x="145" y="300" as="geometry" />
</mxCell>
<mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#424242;fontFamily=Consolas;" value="name = null, age = 0" vertex="1">
<mxGeometry height="25" width="450" x="145" y="325" as="geometry" />
</mxCell>
<mxCell id="18" edge="1" parent="1" source="14" style="endArrow=classic;html=1;rounded=0;strokeWidth=2;strokeColor=#fb8c00;entryX=0.5;entryY=0;entryDx=0;entryDy=0;exitX=0.5;exitY=1;exitDx=0;exitDy=0;" target="19" value="">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="19" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fce4ec;strokeColor=#e91e63;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="70" width="540" x="80" y="385" as="geometry" />
</mxCell>
<mxCell id="20" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#e91e63;strokeColor=none;fontColor=#ffffff;fontSize=16;fontStyle=1;" value="4" vertex="1">
<mxGeometry height="40" width="40" x="95" y="400" as="geometry" />
</mxCell>
<mxCell id="21" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;fontStyle=1;fontColor=#c2185b;" value="父类构造链" vertex="1">
<mxGeometry height="25" width="120" x="145" y="395" as="geometry" />
</mxCell>
<mxCell id="22" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#424242;fontFamily=Consolas;" value="Object() → ... → 直接父类构造()" vertex="1">
<mxGeometry height="25" width="450" x="145" y="420" as="geometry" />
</mxCell>
<mxCell id="23" edge="1" parent="1" source="19" style="endArrow=classic;html=1;rounded=0;strokeWidth=2;strokeColor=#e91e63;entryX=0.5;entryY=0;entryDx=0;entryDy=0;exitX=0.5;exitY=1;exitDx=0;exitDy=0;" target="24" value="">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="24" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#f3e5f5;strokeColor=#9c27b0;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="70" width="540" x="80" y="480" as="geometry" />
</mxCell>
<mxCell id="25" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#9c27b0;strokeColor=none;fontColor=#ffffff;fontSize=16;fontStyle=1;" value="5" vertex="1">
<mxGeometry height="40" width="40" x="95" y="495" as="geometry" />
</mxCell>
<mxCell id="26" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;fontStyle=1;fontColor=#7b1fa2;" value="显式初始化 + 初始化块" vertex="1">
<mxGeometry height="25" width="200" x="145" y="490" as="geometry" />
</mxCell>
<mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#424242;" value="执行属性声明时的赋值和 {} 初始化块" vertex="1">
<mxGeometry height="25" width="450" x="145" y="515" as="geometry" />
</mxCell>
<mxCell id="28" edge="1" parent="1" source="24" style="endArrow=classic;html=1;rounded=0;strokeWidth=2;strokeColor=#9c27b0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;exitX=0.5;exitY=1;exitDx=0;exitDy=0;" target="29" value="">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="29" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e0f2f1;strokeColor=#00897b;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="70" width="540" x="80" y="575" as="geometry" />
</mxCell>
<mxCell id="30" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#00897b;strokeColor=none;fontColor=#ffffff;fontSize=16;fontStyle=1;" value="6" vertex="1">
<mxGeometry height="40" width="40" x="95" y="590" as="geometry" />
</mxCell>
<mxCell id="31" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;fontStyle=1;fontColor=#00695c;" value="构造方法体" vertex="1">
<mxGeometry height="25" width="120" x="145" y="585" as="geometry" />
</mxCell>
<mxCell id="32" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#424242;fontFamily=Consolas;" value="this.name = "张三"; this.age = 20;" vertex="1">
<mxGeometry height="25" width="450" x="145" y="610" as="geometry" />
</mxCell>
<mxCell id="33" edge="1" parent="1" source="29" style="endArrow=classic;html=1;rounded=0;strokeWidth=2;strokeColor=#00897b;entryX=0.5;entryY=0;entryDx=0;entryDy=0;exitX=0.5;exitY=1;exitDx=0;exitDy=0;" target="34" value="">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="34" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#eceff1;strokeColor=#546e7a;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="70" width="540" x="80" y="670" as="geometry" />
</mxCell>
<mxCell id="35" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#546e7a;strokeColor=none;fontColor=#ffffff;fontSize=16;fontStyle=1;" value="7" vertex="1">
<mxGeometry height="40" width="40" x="95" y="685" as="geometry" />
</mxCell>
<mxCell id="36" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;fontStyle=1;fontColor=#37474f;" value="返回对象引用" vertex="1">
<mxGeometry height="25" width="120" x="145" y="680" as="geometry" />
</mxCell>
<mxCell id="37" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#424242;" value="将堆内存地址赋给栈中的引用变量" vertex="1">
<mxGeometry height="25" width="450" x="145" y="705" as="geometry" />
</mxCell>
<mxCell id="38" parent="1" style="text;html=1;strokeColor=none;fillColor=#4caf50;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=1;fontSize=14;fontStyle=1;fontColor=#ffffff;" value="✓ 对象创建完成" vertex="1">
<mxGeometry height="40" width="160" x="270" y="770" as="geometry" />
</mxCell>
<mxCell id="39" edge="1" parent="1" source="34" style="endArrow=classic;html=1;rounded=0;strokeWidth=2;strokeColor=#4caf50;entryX=0.5;entryY=0;entryDx=0;entryDy=0;exitX=0.5;exitY=1;exitDx=0;exitDy=0;" target="38" value="">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="40" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=24;" value="📦" vertex="1">
<mxGeometry height="40" width="40" x="570" y="110" as="geometry" />
</mxCell>
<mxCell id="41" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=24;" value="💾" vertex="1">
<mxGeometry height="40" width="40" x="570" y="205" as="geometry" />
</mxCell>
<mxCell id="42" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=24;" value="0️⃣" vertex="1">
<mxGeometry height="40" width="40" x="570" y="300" as="geometry" />
</mxCell>
<mxCell id="43" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=24;" value="⬆️" vertex="1">
<mxGeometry height="40" width="40" x="570" y="395" as="geometry" />
</mxCell>
<mxCell id="44" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=24;" value="📝" vertex="1">
<mxGeometry height="40" width="40" x="570" y="490" as="geometry" />
</mxCell>
<mxCell id="45" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=24;" value="🔧" vertex="1">
<mxGeometry height="40" width="40" x="570" y="585" as="geometry" />
</mxCell>
<mxCell id="46" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=24;" value="🔗" vertex="1">
<mxGeometry height="40" width="40" x="570" y="680" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
8.3 最佳实践清单
<mxfile>
<diagram id="constructor-best-practices" name="构造方法最佳实践">
<mxGraphModel dx="1281" dy="581" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="800" math="0" shadow="0">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="2" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#64748B;strokeWidth=2;shadow=1;" value="" vertex="1">
<mxGeometry height="750" width="860" x="20" y="20" as="geometry" />
</mxCell>
<mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=24;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="📋 构造方法最佳实践" vertex="1">
<mxGeometry height="60" width="860" x="20" y="20" as="geometry" />
</mxCell>
<mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#D1FAE5;strokeColor=#10B981;strokeWidth=3;shadow=1;" value="" vertex="1">
<mxGeometry height="310" width="820" x="40" y="100" as="geometry" />
</mxCell>
<mxCell id="5" parent="1" style="text;html=1;strokeColor=none;fillColor=#10B981;align=center;verticalAlign=middle;fontSize=20;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="✅ DO(推荐做法)" vertex="1">
<mxGeometry height="50" width="820" x="40" y="100" as="geometry" />
</mxCell>
<mxCell id="6" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ECFDF5;strokeColor=#34D399;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="55" width="380" x="60" y="165" as="geometry" />
</mxCell>
<mxCell id="7" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#10B981;strokeColor=#047857;strokeWidth=2;fontColor=#FFFFFF;fontSize=16;fontStyle=1;" value="1" vertex="1">
<mxGeometry height="32" width="32" x="75" y="176" as="geometry" />
</mxCell>
<mxCell id="8" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=15;fontColor=#047857;fontStyle=1;" value="始终显式保留无参构造方法" vertex="1">
<mxGeometry height="32" width="310" x="115" y="176" as="geometry" />
</mxCell>
<mxCell id="9" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ECFDF5;strokeColor=#34D399;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="55" width="380" x="460" y="165" as="geometry" />
</mxCell>
<mxCell id="10" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#10B981;strokeColor=#047857;strokeWidth=2;fontColor=#FFFFFF;fontSize=16;fontStyle=1;" value="2" vertex="1">
<mxGeometry height="32" width="32" x="475" y="176" as="geometry" />
</mxCell>
<mxCell id="11" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=15;fontColor=#047857;fontStyle=1;" value="使用 this() 委托调用避免重复代码" vertex="1">
<mxGeometry height="32" width="310" x="515" y="176" as="geometry" />
</mxCell>
<mxCell id="12" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ECFDF5;strokeColor=#34D399;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="55" width="380" x="60" y="235" as="geometry" />
</mxCell>
<mxCell id="13" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#10B981;strokeColor=#047857;strokeWidth=2;fontColor=#FFFFFF;fontSize=16;fontStyle=1;" value="3" vertex="1">
<mxGeometry height="32" width="32" x="75" y="246" as="geometry" />
</mxCell>
<mxCell id="14" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=15;fontColor=#047857;fontStyle=1;" value="在构造方法中进行参数校验" vertex="1">
<mxGeometry height="32" width="310" x="115" y="246" as="geometry" />
</mxCell>
<mxCell id="15" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ECFDF5;strokeColor=#34D399;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="55" width="380" x="460" y="235" as="geometry" />
</mxCell>
<mxCell id="16" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#10B981;strokeColor=#047857;strokeWidth=2;fontColor=#FFFFFF;fontSize=16;fontStyle=1;" value="4" vertex="1">
<mxGeometry height="32" width="32" x="475" y="246" as="geometry" />
</mxCell>
<mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=15;fontColor=#047857;fontStyle=1;" value="参数过多时考虑建造者模式" vertex="1">
<mxGeometry height="32" width="310" x="515" y="246" as="geometry" />
</mxCell>
<mxCell id="18" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ECFDF5;strokeColor=#34D399;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="55" width="780" x="60" y="305" as="geometry" />
</mxCell>
<mxCell id="19" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#10B981;strokeColor=#047857;strokeWidth=2;fontColor=#FFFFFF;fontSize=16;fontStyle=1;" value="5" vertex="1">
<mxGeometry height="32" width="32" x="75" y="316" as="geometry" />
</mxCell>
<mxCell id="20" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=15;fontColor=#047857;fontStyle=1;" value="构造方法尽量简洁,复杂逻辑提取到工厂方法" vertex="1">
<mxGeometry height="32" width="400" x="115" y="316" as="geometry" />
</mxCell>
<mxCell id="21" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#10B981;strokeColor=#047857;strokeWidth=1;fontColor=#FFFFFF;fontSize=11;fontStyle=1;" value="框架兼容" vertex="1">
<mxGeometry height="24" width="70" x="345" y="180" as="geometry" />
</mxCell>
<mxCell id="22" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#10B981;strokeColor=#047857;strokeWidth=1;fontColor=#FFFFFF;fontSize=11;fontStyle=1;" value="代码复用" vertex="1">
<mxGeometry height="24" width="70" x="755" y="180" as="geometry" />
</mxCell>
<mxCell id="23" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#10B981;strokeColor=#047857;strokeWidth=1;fontColor=#FFFFFF;fontSize=11;fontStyle=1;" value="安全性" vertex="1">
<mxGeometry height="24" width="70" x="345" y="250" as="geometry" />
</mxCell>
<mxCell id="24" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#10B981;strokeColor=#047857;strokeWidth=1;fontColor=#FFFFFF;fontSize=11;fontStyle=1;" value="可读性" vertex="1">
<mxGeometry height="24" width="70" x="755" y="250" as="geometry" />
</mxCell>
<mxCell id="25" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#10B981;strokeColor=#047857;strokeWidth=1;fontColor=#FFFFFF;fontSize=11;fontStyle=1;" value="可维护性" vertex="1">
<mxGeometry height="24" width="70" x="755" y="320" as="geometry" />
</mxCell>
<mxCell id="26" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=28;" value="💡" vertex="1">
<mxGeometry height="50" width="50" x="800" y="95" as="geometry" />
</mxCell>
<mxCell id="27" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEE2E2;strokeColor=#EF4444;strokeWidth=3;shadow=1;" value="" vertex="1">
<mxGeometry height="300" width="820" x="40" y="430" as="geometry" />
</mxCell>
<mxCell id="28" parent="1" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=20;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="❌ DON'T(避免做法)" vertex="1">
<mxGeometry height="50" width="820" x="40" y="430" as="geometry" />
</mxCell>
<mxCell id="29" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF2F2;strokeColor=#F87171;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="55" width="380" x="60" y="495" as="geometry" />
</mxCell>
<mxCell id="30" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#EF4444;strokeColor=#DC2626;strokeWidth=2;fontColor=#FFFFFF;fontSize=16;fontStyle=1;" value="1" vertex="1">
<mxGeometry height="32" width="32" x="75" y="506" as="geometry" />
</mxCell>
<mxCell id="31" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=15;fontColor=#DC2626;fontStyle=1;" value="在构造方法中调用可被重写的方法" vertex="1">
<mxGeometry height="32" width="310" x="115" y="506" as="geometry" />
</mxCell>
<mxCell id="32" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF2F2;strokeColor=#F87171;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="55" width="380" x="460" y="495" as="geometry" />
</mxCell>
<mxCell id="33" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#EF4444;strokeColor=#DC2626;strokeWidth=2;fontColor=#FFFFFF;fontSize=16;fontStyle=1;" value="2" vertex="1">
<mxGeometry height="32" width="32" x="475" y="506" as="geometry" />
</mxCell>
<mxCell id="34" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=15;fontColor=#DC2626;fontStyle=1;" value="在构造方法中执行耗时操作" vertex="1">
<mxGeometry height="32" width="310" x="515" y="506" as="geometry" />
</mxCell>
<mxCell id="35" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF2F2;strokeColor=#F87171;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="55" width="380" x="60" y="565" as="geometry" />
</mxCell>
<mxCell id="36" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#EF4444;strokeColor=#DC2626;strokeWidth=2;fontColor=#FFFFFF;fontSize=16;fontStyle=1;" value="3" vertex="1">
<mxGeometry height="32" width="32" x="75" y="576" as="geometry" />
</mxCell>
<mxCell id="37" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=15;fontColor=#DC2626;fontStyle=1;" value="在构造方法中启动线程" vertex="1">
<mxGeometry height="32" width="310" x="115" y="576" as="geometry" />
</mxCell>
<mxCell id="38" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF2F2;strokeColor=#F87171;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="55" width="380" x="460" y="565" as="geometry" />
</mxCell>
<mxCell id="39" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#EF4444;strokeColor=#DC2626;strokeWidth=2;fontColor=#FFFFFF;fontSize=16;fontStyle=1;" value="4" vertex="1">
<mxGeometry height="32" width="32" x="475" y="576" as="geometry" />
</mxCell>
<mxCell id="40" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=15;fontColor=#DC2626;fontStyle=1;" value="定义与类名相同的普通方法<div>(带 void)</div>" vertex="1">
<mxGeometry height="32" width="310" x="515" y="576.5" as="geometry" />
</mxCell>
<mxCell id="41" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF2F2;strokeColor=#F87171;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="55" width="780" x="60" y="635" as="geometry" />
</mxCell>
<mxCell id="42" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#EF4444;strokeColor=#DC2626;strokeWidth=2;fontColor=#FFFFFF;fontSize=16;fontStyle=1;" value="5" vertex="1">
<mxGeometry height="32" width="32" x="75" y="646" as="geometry" />
</mxCell>
<mxCell id="43" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=15;fontColor=#DC2626;fontStyle=1;" value="让 this() 和 super() 形成循环调用" vertex="1">
<mxGeometry height="32" width="350" x="115" y="646" as="geometry" />
</mxCell>
<mxCell id="44" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EF4444;strokeColor=#DC2626;strokeWidth=1;fontColor=#FFFFFF;fontSize=11;fontStyle=1;" value="NPE风险" vertex="1">
<mxGeometry height="24" width="70" x="345" y="510" as="geometry" />
</mxCell>
<mxCell id="45" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EF4444;strokeColor=#DC2626;strokeWidth=1;fontColor=#FFFFFF;fontSize=11;fontStyle=1;" value="性能问题" vertex="1">
<mxGeometry height="24" width="70" x="755" y="510" as="geometry" />
</mxCell>
<mxCell id="46" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EF4444;strokeColor=#DC2626;strokeWidth=1;fontColor=#FFFFFF;fontSize=11;fontStyle=1;" value="this逃逸" vertex="1">
<mxGeometry height="24" width="70" x="345" y="580" as="geometry" />
</mxCell>
<mxCell id="47" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EF4444;strokeColor=#DC2626;strokeWidth=1;fontColor=#FFFFFF;fontSize=11;fontStyle=1;" value="混淆错误" vertex="1">
<mxGeometry height="24" width="70" x="755" y="580" as="geometry" />
</mxCell>
<mxCell id="48" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EF4444;strokeColor=#DC2626;strokeWidth=1;fontColor=#FFFFFF;fontSize=11;fontStyle=1;" value="编译错误" vertex="1">
<mxGeometry height="24" width="70" x="755" y="650" as="geometry" />
</mxCell>
<mxCell id="49" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=28;" value="⚠️" vertex="1">
<mxGeometry height="50" width="50" x="800" y="425" as="geometry" />
</mxCell>
<mxCell id="50" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#6366F1;strokeColor=#4F46E5;strokeWidth=3;fontColor=#FFFFFF;fontSize=18;fontStyle=1;shadow=1;" value="VS" vertex="1">
<mxGeometry height="60" width="60" x="420" y="370" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
💬 评论